javascript - How to add Backbone Model global function -
i need add function models, can´t how recognize model "this" scope variable. function model can reseted defaults , data-object parameter:
backbone.model.prototype.reset = function( data ){ this.set(this.defaults); $.each( data, function( key, value ){ this.set( key, value ); }); }
your code seems legit. value of this
defined @ runtime , depends on how call method. if use modelinstance.reset()
value of this
modelinstance
.
however, mutating prototype of 3rd party considered bad practice should avoided. better practice create abstract model , have of model implement it.
it like:
var abstracrtmodel = backbone.model.extend({ reset: function (data, options) { this.clear(options); this.set(_.defaults({}, data || {}, this.defaults || {}), options); } });
the api of model.set
permits passing data hash, don't need inner loop.
Comments
Post a Comment