afeld / backbone-nested

A plugin to make Backbone.js keep track of nested attributes - looking for maintainers! https://github.com/afeld/backbone-nested/issues/157
https://afeld.github.com/backbone-nested/
MIT License
443 stars 83 forks source link

Problems with add() and remove() methods #108

Closed ridgeBoy closed 10 years ago

ridgeBoy commented 10 years ago

Hi there,

I'm learning backbone throught the book 'Backbone.js cookbook' from Vadim Mirgorod, and I have some problems with add() and remove() methods from Backbone.nested.

What I am using is a Chrome browser to view a page, and this page has the following libraries:

The code that triggers the error is:

//this is ok var BuyerModel2 = Backbone.NestedModel.extend({ });

//this is ok buyerModel2.set({ 'name.title': 'Mr', 'name.generation': 'II' });

//this is ok buyerModel2.set({ 'addresses': [ {city: 'Brooklyn', state: 'NY'}, {city: 'Oak Park', state: 'IL'} ] });

//this is ok buyerModel2.set({ 'addresses[1].state': 'MI' });

//this is ok buyerModel2.get('addresses[0].state'); // NY buyerModel2.get('addresses[1].state'); // MI

//this crashes buyerModel2.add('addresses', { city: 'Seattle', state: 'WA' });

buyerModel2.get('addresses[2]');// { city: 'Seattle', state: 'WA' }

//this crashes buyerModel2.remove('addresses[1]');

gkatsev commented 10 years ago

What do you mean it crashes? Are you getting any errors? Can you post those? Also, what version of Backbone-Nested are you using along with what version of Backbone?

ridgeBoy commented 10 years ago

Ok, I'm using those libraries:

jquery-1.10.2.js underscore-1.5.2.js backbone-1.1.0.js backbone.mutators-0.4.1.js backbone.memento-4.0.1a.js workflow.js backbone-validation-0.9.0.js backbone-nested-1.1.2.js backbone-relational-0.8.7.js

I attach a capture of my desktop:

captura de pantalla de 2014-01-19 18 02 16

gkatsev commented 10 years ago

Backbone-Nested 1.1.2 isn't compatible with Backbone 1.x. We're in the process of releasing 2.0 which will have backbone 1.x compatibility. Can you please test with that version? It's available in the master branch.

ridgeBoy commented 10 years ago

Hi,

the result is the same (see attachment)

If you need the sources, I can send them to your email.

captura de pantalla de 2014-01-19 18 24 20

Best regards

gkatsev commented 10 years ago

That's weird since we have tests that pass correctly. Can you a working example on something like jsbin.com?

ridgeBoy commented 10 years ago

I don't know if this is correct because it's my first time at jsBin

http://jsbin.com/IBePuwoz/1/edit?html,js,console,output

gkatsev commented 10 years ago

Looks like you forgot to instantiate the model before using it. You had

var BuyerModel2 = Backbone.NestedModel.extend({});

and then you tried to use it but you didn't have:

var buyerModel2 = new BuyerModel2();

before you started using buyerModel2.

Here's an updated jsbin that works: http://jsbin.com/EbaKaPiF/4/edit

ridgeBoy commented 10 years ago

That's it!!!!!!!! :+1: :+1: :+1: :+1:

I suppose it's a mistake from the book

Thank you very much!!!!!

gkatsev commented 10 years ago

No problem. Glad to help. Please open more issues if you get more problems.