javascript - Confusion about Models in Backbone + React application -


here's example uses backbone react.

he defines model: var _todos = new backbone.model();

and adds 2 functions it:

var todostore = _.extend(_todos, {   areallcomplete: function() {     return _.every(_todos.keys(), function(id){       return _todos.get(id).complete;     });   },   getall: function() {     return _todos.tojson();   } }); 

what don't understand why areallcomplete being applied model instead of collection.

shouldn't function in collection of models , check complete attribute.

similarly, expect getall belong collection - of models.

this example seems replace collection model.

maybe don't totally understand how models used.

that example using backbone.model in wierd way in opinion.

this it's adding new todos store:

var id = date.now();   _todos.set(id, {     id: id,     complete: false,     text: text   }); } 

what it's doing setting every todo-item attribute of model, using id attribute name. ends _todos.attributes looking below

{   "1436600629317": {     "id": 1436600629317,     "complete": false,     "text": "foo"   },   "1436600629706": {     "id": 1436600629706,     "complete": false,     "text": "bar"   } } 

that's same output _todos.tojson(). i've no idea why decided implement that, if try using backbone.sync they'd end server api that's not restful. seems strange use backbone without leveraging of things backbone provides. there's reference change event here don't see being used anywhere. reimplement store using regular js object.

the thing example seem using backbone backbone.events in dispatcher. you're totally right using collection make way more sense because make talk rest based server api. example seems use backbone sake of using backbone.


Comments