backbone.js-collections

Backbone fetching data from model vs collection

∥☆過路亽.° 提交于 2019-12-06 12:34:24
问题 I created a simple backbone project, where it fetches all books details and show it in UI. I am fetching all the books details from the model. not at all using collection something like this var BookModel= Backbone.Model.extend({ initialize: function(){ this.fetchData(); }, fetchData: function(){ this.url= "/get/all_books"; this.fetch({ success: success_callback, error: error_callback }) } }); This is working fine. But why do I have to use collections ? If I would have used collection it

How to convert an array to Collection in backbone

一个人想着一个人 提交于 2019-12-06 07:10:09
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 collections. Please suggest. Thanks in advance! A Backbone collection expects a list of models/hashes

Backbone.js - getting id from collection create

早过忘川 提交于 2019-12-06 01:32:53
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( { runningorderid: null, listitemid: 1, starttime: n} , { wait: true } ); console.dir( resp ); console.dir( resp

How To Find Triggered Event From Backbone.listenTo?

我与影子孤独终老i 提交于 2019-12-05 08:41:25
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 the collection and doesn't mention what event is actually triggered. I've tried e.type and e.target but

Backbone Collection of polymorphic Models

喜你入骨 提交于 2019-12-05 02:40:10
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 instantiated as sub-classed models (Monkey,Cat,Dog) and not as Animal. How can you achieve this? From Backbone

Backbone fetching data from model vs collection

感情迁移 提交于 2019-12-04 15:06:32
I created a simple backbone project, where it fetches all books details and show it in UI. I am fetching all the books details from the model. not at all using collection something like this var BookModel= Backbone.Model.extend({ initialize: function(){ this.fetchData(); }, fetchData: function(){ this.url= "/get/all_books"; this.fetch({ success: success_callback, error: error_callback }) } }); This is working fine. But why do I have to use collections ? If I would have used collection it would be something like as follows var BookModel= Backbone.Model.extend({ defaults:{ id:'', name: '',

BackboneJS model.url using collection.url

好久不见. 提交于 2019-12-04 02:59:53
问题 From my understanding the default behavior of Backbone JS Models are to return the Collection's URL, unless the model has a urlRoot specified. I can't seem to get the behavior to work. From the documentation: model.url() ... Generates URLs of the form: "[collection.url]/[id]" by default, but you may override by specifying an explicit urlRoot if the model's collection shouldn't be taken into account. Here is my collection, and model respectively: var MyCollection = Backbone.Collection.extend({

How to place a collection within a model in Backbone.js?

浪尽此生 提交于 2019-12-04 00:17:51
I would like some insight on how one would add structure a collection within a model. My simple app has teams (so a team model and collection of teams) and each team has a bunch of players(player model and player collections). So a visual structure of it is like so: Team A - Player 1 - Player 2 - Player 3 Team B - Player 1 - Player 2 and so on... How would one structure such a backbone app? Here is how I am planning it so far: 1) I would have a Team Collection, that would hold multiple teams whose model property corresponds to the TeamModel. 2) A Player Collection, that would hold all multiple

How to pluck a Backbone collection's attribute

我是研究僧i 提交于 2019-12-03 15:48:48
问题 I want to create an array of specific attribute values from a Backbone collection. var days = _.select( this.collection.models, function(model) { return model.attributes.type === 'session'; } ); days = _.pluck(days, 'attributes'), days = _.pluck(days, 'date'); This works, but seems inefficient. Is there a way to accomplish the same thing without having to define days three times? 回答1: pluck is a convenience method that wraps map , and map is available directly on the collection, which should

Mapping JSON to backbone.js collections

隐身守侯 提交于 2019-12-03 13:04:39
问题 Alright, it looks like I need a hint to point me in the right direction. This question is two part - working with mult-dimensional JSON and Collections of Collections from JSON. Background I have some JSON that is going to be retrieved from a server and have control over how it could be formatted. Multi-Dimentional JSON I'm having some trouble being able connecting the model to the parts in the JSON. Say I wanted to render just each of the posts author name , and the content of status in the