kuba0506
kuba0506

Reputation: 475

Backbone read JSON item and save to model

Hi I'm new to Backbone but have to solve a problem.

There is a working webapp - online shop. On the CartView browsers gets a JSON file from /lampy/api/filter.

I need to save it somehow into model because I need some value from it?

How should I do this?

Thanks in advance.

Upvotes: 0

Views: 35

Answers (1)

garethdn
garethdn

Reputation: 12351

Let's say your JSON object looks like:

{
    item1: 'some string',
    item2: 'another string',
    item3: 'and another'
}

When you say:

to save it somehow into model

I'm assuming you mean instantiate a model with the values of your JSON object. To do this you can simply do:

var myModel = new Backbone.Model(yourJsonObject)

This will just be a plain Backbone Model without any Url so you won't be able to interact with the server with it, but as you said you need some value from it so you can use:

myModel.get('item1')    // returns 'some string'

Upvotes: 1

Related Questions