javascript - Sort through a model(array) in Backbone? -


i'm learning backbone forgive me if title incorrect.

i want sort through model (is model array?, have got wrong?) can't it. in regular js can use function sort through array , know works:

getobjects: function(obj, key, val) {   var objects = [];   (var in obj) {     if (!obj.hasownproperty(i)) continue;     if (typeof obj[i] == 'object') {         objects = objects.concat(getobjects(obj[i], key, val));     } else if (i == key && obj[key] == val) {         objects.push(obj);     }   }   return objects; } 

but don't know should put in backbone. tried putting in view, error saying "undefined not function" getobjects.

window.winelistview = backbone.view.extend({    initialize: function () {     this.render();   },    //sort through json when getting different priority tasks   getobjects: function(obj, key, val) {     var objects = [];     (var in obj) {       if (!obj.hasownproperty(i)) continue;       if (typeof obj[i] == 'object') {         objects = objects.concat(getobjects(obj[i], key, val));       } else if (i == key && obj[key] == val) {         objects.push(obj);       }     }     return objects;   },    render: function () {     var wines = this.model.models;     wines = getobjects(wines, 'completed', 'no');      var len = wines.length;     var startpos = (this.options.page - 1) * 8;     var endpos = math.min(startpos + 8, len);      $(this.el).html('<ul class="thumbnails"></ul>');      (var = startpos; < endpos; i++) {       $('.thumbnails', this.el).append(new winelistitemview({model: wines[i]}).render().el);       //$('.thumbnails', this.el).append('<br/>');     }      $(this.el).append(new paginator({model: this.model, page: this.options.page}).render().el);      return this;   } }); 

how can declare getobjects function can accessed? model not array? there different way should sorting? appreciated! thank you.

if familiar concept of objects , arrays in javascript, backbone model , collection analogous wrapper objects js objects , arrays respectively.

js objects bunch of key-value pairs, backbone models store key-value data in attributes field. if want multiple items of type in arrays, need backbone collections.

backbone models have 1 key each parameter want work with, example if have person model, have 1 possible value firstname. can have collection of person called persons , sort collection firstname.

to sort in backbone, need specify comparator collection , call sort on collection.

for example

this.collection.comparator = function(person) {             return person.get('firstname');  } this.collection.sort(); 

Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -