backbone.js-collections

Backbone.js What is the purpose of specifying a model in collection

旧时模样 提交于 2019-12-01 13:29:28
Here is what I am trying to understand. Often times I find myself writing backbone like this: var CallModel = Backbone.Model.extend({ }); var CallsCollection = Backbone.Collection.extend({ model: CallModel, url: 'url/to/external/json' }); It is a very basic example but as you can see, there is nothing really in the model all the data is coming into the Collection via an external url call to a json file that is build from a database. So whats the purpose of the model? I am sure that I am probably not using backbone.js to its fullest extent which is why I am here asking you guys. T J First of

Backbone.js - fetch method does not fire reset event

南笙酒味 提交于 2019-11-30 13:38:22
问题 I'm beginning with Backbone.js and trying to build my first sample app - shopping list. My problem is when I fetch collection of items, reset event isn't probably fired, so my render method isn't called. Model: Item = Backbone.Model.extend({ urlRoot : '/api/items', defaults : { id : null, title : null, quantity : 0, quantityType : null, enabled : true } }); Collection: ShoppingList = Backbone.Collection.extend({ model : Item, url : '/api/items' }); List view: ShoppingListView = Backbone.View

Fetch a collection using a POST request?

柔情痞子 提交于 2019-11-30 07:56:39
I have managed to work with REST API's to fetch() data where the urls contain minimal parameters (and use GET ). How would one retrieve a collection through a POST request? Also note that fetch supports Jquery.ajax parameters, so you can easily set type = post in the call. Messages.fetch({data: {api_key: 'secretkey'}, type: 'POST'}); For more parameters: http://api.jquery.com/jQuery.ajax/ You may need to extend the Collection object to install your own convention for fetches. In doing so, you would likely provide your own fetch function. Something like: fetch : function(options) { options ||

Backbone.js - fetch method does not fire reset event

风流意气都作罢 提交于 2019-11-30 07:44:43
I'm beginning with Backbone.js and trying to build my first sample app - shopping list. My problem is when I fetch collection of items, reset event isn't probably fired, so my render method isn't called. Model: Item = Backbone.Model.extend({ urlRoot : '/api/items', defaults : { id : null, title : null, quantity : 0, quantityType : null, enabled : true } }); Collection: ShoppingList = Backbone.Collection.extend({ model : Item, url : '/api/items' }); List view: ShoppingListView = Backbone.View.extend({ el : jQuery('#shopping-list'), initialize : function () { this.listenTo(this.model, 'reset',

How do I implement the Backbone model in latest Parse js sdk (1.6.2)?

99封情书 提交于 2019-11-29 14:28:16
The Parse Collection class corresponding to Backbone.Collection is no more in the SDK. It used to be there in 1.2.13. As of September 9th, 2015, the Parse Javascript SDK no longer contains backbone specific behavior. You can reference a previous version of the JS SDK ( http://www.parsecdn.com/js/parse-1.5.0.min.js ) to access the backbone specific behaviors but understand that branch is no longer maintained. V1.6.0 — SEPTEMBER 9, 2015 New Features Designed to work with ES6. Parse.Object can be treated as a class, and can be subclassed with the extend keyword When subclassing Parse.Object, you

Fetch a collection using a POST request?

徘徊边缘 提交于 2019-11-29 10:50:23
问题 I have managed to work with REST API's to fetch() data where the urls contain minimal parameters (and use GET ). How would one retrieve a collection through a POST request? 回答1: Also note that fetch supports Jquery.ajax parameters, so you can easily set type = post in the call. Messages.fetch({data: {api_key: 'secretkey'}, type: 'POST'}); For more parameters: http://api.jquery.com/jQuery.ajax/ 回答2: You may need to extend the Collection object to install your own convention for fetches. In

Backbone Collection with multiple models?

谁都会走 提交于 2019-11-28 18:54:30
I'm learning Backbone. I want to create a list that can contain different models, with different attributes. For example, listing folder contents, which could include models of type file and models of type folder, in any order. file : { title : "", date : "", type : "", yaddayadda : "" } folder : { title : "", date : "", haminahamina : "" } What is the proper way to represent this in Backbone? Is it possible to have a single Collection with multiple models? Create a base model that your other models inherit from: var DataModel = Backbone.Model.extend({ // Whatever you want in here }); var

Filter backbone collection by attribute value

旧街凉风 提交于 2019-11-28 16:20:48
I have a defined model and a collection: var Box = Backbone.Model.extend({ defaults: { x: 0, y: 0, w: 1, h: 1, color: "black" } }); var Boxes = Backbone.Collection.extend({ model: Box }); When the collection is populated with the models, I need a new Boxes collection made out of Box models that have a specific color attribute contained in the complete collection, I do it this way: var sorted = boxes.groupBy(function(box) { return box.get("color"); }); var red_boxes = _.first(_.values(_.pick(sorted, "red"))); var red_collection = new Boxes; red_boxes.each(function(box){ red_collection.add(box);

Best practice for saving an entire collection?

♀尐吖头ヾ 提交于 2019-11-28 04:54:55
Say that I have a Collection and I've made changes to many of its Models. What's the best way to save all of the changes using a single HTTP request? Usually REST backends handle single instance creation/update. You would need to change that to accept an array of objects. That said, on the client side, you would need to go directly to the Backbone.sync function Backbone.sync = function(method, model, options) In this case your model should be an array of model. The method should be "create" or "save" and the options take the same type of options as a jQuery ajax call (error, success, etc.) I'm

How to find a model from a collection according to some attribute other than the ID?

馋奶兔 提交于 2019-11-28 04:28:50
I have a model with several object: //Model Friend = Backbone.Model.extend({ //Create a model to hold friend attribute name: null, }); //objects var f1 = new Friend({ name: "Lee" }); var f2 = new Friend({ name: "David"}); var f3 = new Friend({ name: "Lynn"}); and also, I will add these friends object to a collection: //Collection Friends = Backbone.Collection.extend({ model: Friend, }); Friends.add(f1); Friends.add(f2); Friends.add(f3); and now I want to get a model according to the name of the Friend. I know that I can add an ID attribute to achieve this. But I think there should have some