Hello,

lets say I have to collections - x and y. I know that - when first collection is updated, the second collection has new data and also needs to be updated.

So I now did x.fetch and y.fetcg which generates 2 ajax queries.

BUt such things when I used to do without backbone - I just send one quyery and it returrs 2 arrays.
I think its more optimal to use one query, becuase sending takes time. Of course thats not a big issue in my case, but still its bit weird when you are forced to do things in less optimal way.

How do you solve this?

Member Avatar for LastMitch

BUt such things when I used to do without backbone - I just send one quyery and it returrs 2 arrays.
I think its more optimal to use one query, becuase sending takes time. Of course thats not a big issue in my case, but still its bit weird when you are forced to do things in less optimal way.

What about AJAX? To me the question you mention is a bit hard to understand because there's no code just a question that has many possibity.

You can used this:

http://api.jquery.com/jQuery.getJSON/

The simple code:

function fetch() {
    $.getJSON("url:big.json", {
        success: function (response) {
            for (var fetch in collection) {
                collection[x.fetch] = new collection[x.fetch](response[y.fetch]);
            }
        }
    });
}

fetch();

I was talink in general. Lets use example this:
http://backbonejs.org/docs/todos.html

there you can find line

Todos.fetch();

and it gets todo list items. It uses same ajax, its in fetch function in backbone.js. And it automatically assigns the array to the Todos collection because we call from Todos object.

But what if there is another collection, lets say Cars.

Then when I do

Todos.fetch()

and I know when there is new Todos there are also new Cars.

So I would need to call fetch 2 times and it makes to AJAX calls.

Todos.fetch();
Cars.fetch();

Thats what I dont like. And not sure what is the best way to solve this. Hmm, maybe override fetch function and in Todos fetch update both Cars and Todos collections.

Member Avatar for LastMitch

Thats what I dont like. And not sure what is the best way to solve this. Hmm, maybe override fetch function and in Todos fetch update both Cars and Todos collections.

Maybe I mis-understand your question in the begininng. I thought you want to find another option not using backbone.js so I suggest Ajax.

Your issue is bit to advance for me. Sorry can't help anymore.

I found this website maybe you have read it before or maybe not you can take a look at it:

http://chilipepperdesign.com/2013/01/15/backbone-js-bind-callback-to-successful-model-fetch/

np, thaks for try :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.