Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Merge attributes when using model.set with Backbone Associations

Backbone-associations updates existing nested model if id's of new and existing model match. If id's are undefined or they don't match, then the nested model gets replaced by a new one.

What I do for these singleton nested models is I introduce fake id=0 and then it works like expected.

Here is a working jsfiddle.

Working code:

// define the Layout model
var Layout = Backbone.AssociatedModel.extend({
    defaults: {
        id: 0    

// define the User model, with layout as a Related
var User = Backbone.AssociatedModel.extend({
    relations: [
            type: Backbone.One,
            key: 'layout',
            relatedModel: Layout
    defaults: {
        layout: {}

// create a new user
var user = new User({ user_name: 'pascalpp' });

// set a property on the layout model
user.set('', 'bar');
user.get(''); // returns 'bar'

// call set on the user directly, passing a JSON
structure with no foo property
user.set({ layout: { id:0, 'baz': 'bing' } });
user.get(''); // foo got wiped, so this
returns undefined


Categories : Javascript

Related to : Merge attributes when using model.set with Backbone Associations
Backbone model when created already has attributes
I see what's going on now, in your teams collection initialize method you have this line: this.set('members', new App.Collections.Users(members));` So this is calling set on a collection which is different from calling set on an individual model. On a collection set treats the first element as an array of models. You are passing 'members' as the first parameter and this adding a model to the

Categories : Javascript
Ruby on Rails: nested attributes, create a model and its nested attributes use the main model to create more nested attribute models using remote
I tried the following in the create action of order controller and it worked. If anyone have a better solution please let me know. def create if session[:order_id] @order = Order.find(session[:order_id]) @order.update_attributes(item_params) else @order = end session[:order_id] = respond_to do |forma

Categories : Ruby On Rails
Understanding Rails Model Associations
Rails Guides provides an explanation as to why you're experiencing the problem. Essentially, when you declare a belongs_to relationship, the foreign key appears on the table for the class declaring it. When you declare a has_one relationship, the foreign key is on the table for the class in the declaration. Example In this scenario, the pictures table would require a page_id foreign key. class

Categories : Ruby On Rails
How to use exclude_properties and include_properties to exclude/include SQLAlchemy model attributes from corresponding Spyne model?
This doesn't directly answer your question, but please consider the following code: from spyne import * TableModel = TTableModel() class SomeClass(TableModel): __tablename__ = 'some_table' id = Integer(pk=True) s = Unicode i = Integer(exc_table=True) Here, pk stands for primary key (you can use the long form primary_key if you wish) and the i attribute will just be ignored by

Categories : Python
Sails.js associations: "Many" side of one-to-many associations not saving to MongoDB or Postgres (vs. disk db)
The .create method doesn't return the populated data in it's callback. If you query the Feed model and populate the posts association you should see the data. Feed.find() .populate('posts') .exec(console.log);

Categories : Node Js
Recently Add
How to get selected text with JavaScript?
Non Modal Alert Messages in java script, activated by a button
How to Count [Combined] line length of multiple files?
Is there any javascript event fired when the on-screen keyboard on mobile safari or chrome opens?
Datatables sort by time ago
Internet Explorer getClientRects absolute positioned elements
How can I transform a CamanJS Filter into a KineticJS Filter?
AngularJS : index not updating after removing array item(s) by index
Slick slideshow in ember.js template
Recursive function with defer
Unable to find module mongodb in simple node.js app
Why does function return false after checking only one value in loop
Detect if the mouse is over an element
Placing and rotating svg elements along a path with Snap.svg
YouTube Asynchronous function
Cordova App: Stop JavaScript from running when App is closed
how to define a variable in an javascript ifstatement
display dd on click using jquery
jQuery Timer Countdown within Session
setInterval on click event to run for 10 seconds
Are syntax errors raised in JavaScript during Tokenizing or Parsing?
Can i perform form based http authentication for my TP-Link Wireless Router(IP:
Blocked frame in reCapcha
Updated property not recognized by $watch
Template Missing error on update only
nodejs object declaration and immediate shorthand if statement crashes the app
Hide / show dynamically generated table columns with right / left animation
Convert UTC Date to Local Date in JavaScript
Position DOM elements without CSS
modifying mergesort to count inversions in JavaScript
© Copyright 2017 Publishing Limited. All rights reserved.