I've tried:
initialize: function() {
if (this.get("id") == "modelOfInterest") {
var func = function() {
//do some stuff with the model
}
_.bind(func, this)
}
}
and
initialize: function() {
if (this.get("id") == "modelOfInterest") {
var func = function() {
//do some stuff with the model
}
this.on("func", func, this);
}
}
However in both cases:
myModelInstance.func(); //object has no method func
I'd prefer not to use _.bindAll()
.
I've edited the code above to show that I am trying to bind func to only one model. The model is initialize when it is added to a collection : all the models fire initialize at the same time and I just want to bind func to one of them.
Any reason not to do the obvious?
Model = Backbone.Model.extend({
func: function() {
},
})
Assign func
as a property of your model in your if
block.
var Model = Backbone.Model.extend({
initialize:function() {
if (this.get('id') === 1) {
this.func = function() {
// your logic here
};
this.on('func',this.func,this);
}
}
});
Static methods should be declared on a second dictionary within the .extend call:
SomeModel = Backbone.Model.extend({
initialize: function(){}
},{
someStaticFunction: function(){
//do something
}
});
Try this:
SomeModel = Backbone.Model.extend({
initialize: function(){},
someFunction: function(){
//do something
}
});
And this:
var model = new SomeModel();
model.someFunction();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.