backbone.js-collections

Returning a Backbone Collection and iterating through - not with a View

馋奶兔 提交于 2019-12-11 22:19:11
问题 I am returning a collection of objects returned from a Mongodb database and want to find how many models were returned and be able to iterate through them and create some DOM elements based on that. I am not using a Backbone.View and will be using Famo.us. I create a collection and get it returned BUT I can't seem to access the models inside or their data - wondered if you could help :) Here is the Model/Collection: define( function (require, exports, module) { 'use strict'; var Backbone =

Association between Backbone Collection and XHR object created when fetching

梦想的初衷 提交于 2019-12-11 08:22:15
问题 Is there an existing association between a Backbone Collection and the XHR object created when you execute a .fetch() on it? I would like to do something like this: collection = new Backbone.Collection; xhrObj = collection.fetch(); xhrObj.parent == collection; //true The larger goal is for me to check to see if there are any pending .fetch() 's for a specific collection. If there is another way to do this in Backbone, please let me know. I figured I would just store XHR objects and check if

How can I get a list of models from the collection that have changes since constuction

为君一笑 提交于 2019-12-11 08:16:37
问题 If I initialize a collection with an array of objects for the model attributes when a dialogview is initialized. Then the dialog view that lets the user edit the list updates those model values with calls to model set. When the dialog's ok button is clicked, Does backbone provide a way to get the list of only those models that changed since the collection was created/initialized? 回答1: There are various model methods that look tempting: Model#hasChanged Model#changedAttributes Model#previous

How to re fetch backbone collection after collection create?

余生颓废 提交于 2019-12-11 06:15:46
问题 Can someone tell me how to re fetch a Backbone collection after calling collection's create function when I create a new model? When I call fetch on my collection after creating new model, sometimes I'm getting that model and sometimes not. My problem is when I create a new model in my collection, I'm not getting the id back of my model and then I can't update it immediately, I need to refresh the page and then I got the id of the created model. I tried with listenTo but I can't use it

Backbone: Show validation errors for each model in a collection on VIEW

牧云@^-^@ 提交于 2019-12-11 03:57:10
问题 I'm working on a Backbone application where I allow the user to Add multiple items. Here's my Model: //Model var Item = Backbone.Model.extend({ defaults: { part1: 'hello', part2: 'world' }, validate: function (attr, options) { var error = ''; //console.log(attr); if(attr.part2 == "world1"){ this.trigger('err:world1'); error = 'world 1 error'; } if(attr.part2 == "world3"){ this.trigger('err:world3'); error = 'world 3 error'; } } }); Collection: //Collection var List = Backbone.Collection

Can't loop over backbone collection

强颜欢笑 提交于 2019-12-11 01:56:03
问题 It seems that I can't loop through a backbone collection. I've seen this topic in several threads, but none of those solutions help. render:function () { this.$el.html(this.template(this.model.attributes)); var that = this; console.log(this.projects.models); _.each(this.projects.models, function(model) { console.log(model); }); return this; } From this my console only shows Array[2] I would expect to see each model as well. Does anyone know what I'm doing wrong here ? 回答1: To get your

How to convert an array to Collection in backbone

可紊 提交于 2019-12-10 10:44:01
问题 I have an array defined as: this.noOfHouseHold = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]; I'm trying to convert it to a Backbone Collection as: var adultListCollection = new Backbone.Collection(this.noOfHouseHold); It gives me the list but for 2 digit numbers, it shows something like this: attributes: Object 0: "1" 1: "1" I'm not able to understand as to what is wrong here or is it the wrong way I'm converting my array to

Backbone.js - getting id from collection create

放肆的年华 提交于 2019-12-07 16:33:01
问题 I am adding a model to a collection using the create method and the api is responding just fine. The model seems to have been properly returned and see the console.dir( resp ); which is what I was looking for. However, when I try to access runningorderid , which is the id as defined with idAttribute , the response is null. I presume this is something to do with the async nature of the response, but I don't know how to deal with it. var resp = window.app.RunningOrderCollection.create( {

How To Find Triggered Event From Backbone.listenTo?

帅比萌擦擦* 提交于 2019-12-07 04:03:08
问题 In Backbone, I'm using the new Backbone.listenTo event. One of my instances has the listener attached to three different events e.g.: this.listenTo(this._Collection, 'reset add change', this._setCollection); It is called appropriately when it's supposed to and there are no issues there. What I don't know is how to find out which event was triggered. I have access to the e argument using: _setCollection: function(e) { // Do fun stuff } The problem is that the e argument only sends a copy of

Backbone Collection of polymorphic Models

流过昼夜 提交于 2019-12-06 21:28:46
问题 I have a collection of Animals. App.Collections.Animals extends Backbone.Collection model: App.Animal url: '/animals/' #returns json And these animal classes: App.Models.Animal extends Backbone.Model App.Models.Monkey extends App.Models.Animal defaults:{type:'Monkey'} App.Models.Cat extends App.Models.Animal defaults:{type:'Cat'} App.Models.Dog extends App.Models.Animal defaults:{type:'Dog'} When collection is filled with JSON (records contain the type attribute) I want models to be