One. New
var model_1 = new Backbone.model ({' name ': ' Hello '});
var model_2 = new Backbone.model ({' name ': ' Hi '});
var models = new Backbone.collection ();
Models.add (Model_1);
Models.add (model_2);
Alert (Json.stringify (models));
Two. Instantiation of the model
var M = Backbone.Model.extend ({
Aaa:function () {
alert (123);
}
});
var childm = M.extend ();
var model = new Childm;
MODEL.AAA ();
Three. How to listen for events on the model
$ (function () {
var M = Backbone.Model.extend ({
Defaults: {
Name: ' Hello '
}
});
var V = Backbone.View.extend ({
Initialize:function () {
This.listento (This.model, ' Change ', this.show);
},
Show:function (model) {
$ (' body '). Append (' <div> ' + model.get (' name ') + ' </div> ');
}
});
var m = new M;
var v = new V ({model:m});
M.set (' name ', ' Hi ');
});
Four. Model settings and get background data
Backbone.sync = function (method, model) {
Alert (method + ":" + json.stringify (model));
};
var C = Backbone.Collection.extend ({
Initialize:function () {
This.on (' Reset ', function () {
alert (123);
});
},
URL: '/users '
});
var models = new C;
Models.fetch ();
Five. Configuration Justification method
var Workspace = Backbone.Router.extend ({
Routes: {
' Help ': ' Help ',//#help
"Search/:query": "Search",//#search/kiwis
"Search/:query/p:page": "Search"//#search/KIWIS/P7
},
Help:function () {
Alert (1);
},
Search:function (query, page) {
Alert (2);
}
});
var w = new Workspace;
Backbone.history.start ();
Six. Methods for view-bound events
$ (function () {
var V = Backbone.View.extend ({
El: $ (' body '),
Events: {
' Click Input ': ' AAA ',
' MouseOver li ': ' BBB '
},
Aaa:function () {
alert (123);
},
Bbb:function () {
alert (456);
}
});
var veiw = new V;
});
Seven. Binding model
$ (function () {
var M = Backbone.Model.extend ({
Defaults: {
Name: ' Hello '
}
});
var V = Backbone.View.extend ({
Initialize:function () {
This.listento (This.model, ' Change ', this.show);
},
Show:function (model) {
$ (' body '). Append (This.template (This.model.toJSON ()));
},
Template: _.template ($ (' #template '). HTML ())
});
var m = new M;
var v = new V ({model:m});
M.set (' name ', ' Hi ');
});
Eight.
201507221403_ "One of the backbone-new models and collections, instantiation models, methods for listening to events on models, model settings and getting back-end data, configuration justification methods, view binding events, binding models, etc."