1 collection:
{
"page":1,
"limit":10,
"title":2,
"books":[
{"id":1,"title":"thinking in java"},
{"id":2,"title":"first head java"}
]
}
var Books = Backbone.Collection.extend({
url:"/books",
parse:function(data){
return data.books
}
})
在申明 model 或者 collection 时就设定好返回的数据 parse
2 events:
2.1 on
// you can set many names split by space like: "change:username change:nick"
Backbone.Events.on("namespace", function(data) {
// do something
})
// 如果绑定在一个模型上, 最后一个参数作为中间显示函数的this使用
model.on("namespace", this.render, view)
// also you can bind plural events like this
Backbone.Events.on({
"change:username": customFunctionA,
"change:email change:phone": customFunctionB
})
// another common syntax in view 在view上使用
events:{
"change:username":customFunctionA,
"change:email change:phone": customFunctionB
}
// call this event
// params[1] is a parameter for function data , can be used in it
Backbone.Events.trigger("namespace", data)
or
customFunctionA()
2.2 off
unbind function
// Removes just the `customFunctionA` callback
object.off("change", customFunctionA)
// Removes all "customFunctionA" callbacks
object.off("change")
// Removes the `customFunctionA` callback for all events
object.off(null, customFunctionA)
// Removes all callbacks for `context` for all events
object.off(null, null, context)
// Removes all callbacks on `object`
object.off();
2.3 listenTo
// 视图监听 如果触发指定模型的某一绑定事件 则更新视图
view.listenTo(model, "change:title", view.render)
3 model #
// 覆盖父类方法
// 在java或其他高级语言中可以使用supper method调用父类方法 但是js不行 只能用以下折中方法
var books = Backbone.Model.extend({
set: function(args, options) {
Backbone.Model.prototype.set.apply(this, args)
...
}
})