Just started Backbone.js
and following tuorials at backbonejs.org.
Here is what I wanted to achieve. I created a model which asks for width and saves it. Now I have used listenTo
with object
I have created as below on model
to read the changed property value. It triggered the change
event
, but, I always get undefined
for width. Code is below.
var mod = Backbone.Model.extend({
askColor:function() {
var w = prompt("Enter width");
this.set({ width:w})
}
});
var model = new mod();
model.set({width:1234})
var obj = {};
_.extend(obj,Backbone.Events);
obj.listenTo(model, 'change', function() {
alert("width = "+model.width)
})
model.askColor();
I really don't know what am I missing in the code or am I doing wrong? Please help out a beginner....
The width
attribute is actually stored inside model.attributes
. You should use .get()
to get the width
attribute.
obj.listenTo(model, 'change', function() {
alert("width = "+model.get('width'))
})
You can also use model.attributes.width
alongwith model.get('width')
.
Also following is the best tutorial I found when I started to learn Backbone.js
: http://adrianmejia.com/blog/2012/09/11/backbone-dot-js-for-absolute-beginners-getting-started/
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.