backbone-model

backbone.js adding model to collection adds to all models in collection

随声附和 提交于 2020-01-14 12:12:30
问题 Playing around with backbone.js, so far I've created models and collections for a deck of cards and two players. The problem is when I try move a card from the deck and add it to the player's hand the card gets added to all players hands. Here's my offending code, hopefully a boffin will spot the mistake straight away: //the first card goes to player1 var topCard = deck.at(0); //A of hearts deck.remove(topCard); var hand = players.at(0).get("hand"); hand.add(topCard); //the second card goes

Loading collections into models in backbone.js

六眼飞鱼酱① 提交于 2020-01-06 15:07:29
问题 Here's the goal: I have a parent model. It is in a collection called parents . In a parent is a collection of children . So, when I instantiate the parents collection, I get the parent models, each with the collection of children in them. I've tried doing this several different ways, and I'm getting different performance. Sometimes the children don't show up at all. Sometimes, they are repeated for each iteration of the parent render loop. I'm looking for help on what the best practice is for

Backbone destroy method is triggering post call instead of delete when type: “DELETE”, is not passed

删除回忆录丶 提交于 2019-12-24 11:17:53
问题 var asset = Backbone.Model.extend({ urlRoot: 'https://www.example.com/asset/', idAttribute: "assetId", }); var k = new asset({"assetId":11174355100070}); k.destroy({ headers : { 'Authorization': 'OAuth ' + oauthInfo.oa2, 'GSID' : oauthInfo.GSID }, contentType: "application/json", success: function(){console.log("success method")}, error: function(){console.log("failure error")} }); Why is the backbone destroy method triggering the post call instead of delete? But when type: "DELETE" is passed

listen to a collection add/change as a model attribute of a view

半腔热情 提交于 2019-12-22 09:09:40
问题 I have a Measure View, and it has an associated Measure Model, which has two collections, RepresentationsCollection and BeatsCollection. The Measure View has nested child Representation views, each with their own representation model, and all representation models share the same attribute of a reference to the Measure View's BeatsCollection. I know that if you are listening to a change event, the collection won't respond when something is added. You are supposed to use bind. The docs aren't

Easiest way to reset Backbone's model to initial defaults?

随声附和 提交于 2019-12-20 08:38:35
问题 My models already have a defaults hash. When parts of the view/page are reset, I wish to reset the models back to their original defaults. Currently, I explicitly set each attribute to its default value. Is there anything built in or a JavaScript/Underscore.js/Backbone.js/jQuery function that I could use to do this in a single statement? 回答1: myModel.clear().set(myModel.defaults); 回答2: I do this when the model has non-null initial object properties. first, define defaults as a function var

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

社会主义新天地 提交于 2019-12-19 11:15:31
问题 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

Nested Models in Backbone.js, how to approach

一世执手 提交于 2019-12-17 15:01:11
问题 I've got the following JSON provided from a server. With this, I want to create a model with a nested model. I am unsure of which is the way to achieve this. //json [{ name : "example", layout : { x : 100, y : 100, } }] I want these to be converted to two nested backbone models with the following structure: // structure Image Layout ... So I define the Layout model like so: var Layout = Backbone.Model.extend({}); But which of the two (if any) techniques below should I use to define the Image

how to unset an _id backbone model attribute?

旧时模样 提交于 2019-12-13 06:00:10
问题 I would like to unset() the _id attribute from an instance of a model, to make a POST request using the save() model method . But i get a Uncaught TypeError: Object [object Object] has no method 'call' backbone-min.js because of this line: myModel.unset('_id'); I am using idAttribute: "_id" so i tried: myModel.unset('id'); But it doesn't unset the _id attribute. 回答1: Using model.unset('_id') should work fine. My guess is that the error is thrown by a change event listener, either in your code

backbone.stickit and html-form: How to save (patch) only changed attributes?

烈酒焚心 提交于 2019-12-13 04:06:20
问题 tl;dr How to use backbone.stickit with a html form to change an existing model fetched from the server and only PATCH the changed attributes (changed by user input within the html form) to the server? /tl;dr I'm using backbone.stickit in a backbone.js application to bind a model to a HTML-form which is part of a backbone view. This works fine so far, but it becomes a little bit complicated if I'm going to save the bound model. This is because I want to use the PATCH-method and only send the

Render a View of Backbone Model returns undefined

廉价感情. 提交于 2019-12-12 01:43:22
问题 Lets say i have a JSON like this: JSON example, my json is validated on jsonlint and it works. json_object = { "texts_model": { "hello": "hola", "icon_text": "Icon!" }, "collection_vias": { "text": "hola", "icon_text": "Icon!" } }; I have made a Collection that parse the contents of the json and generates model and collections from this json. App.TemplatesCollection = Backbone.Model.extend({ model: App.TemplateModel, url: TEMPLATES_SERVICE, initialize: function(){ this.fetch({ success: