0

I'm writing basically a folder browser in JavaScript, that starts at some folder on my server and every time you click a file I want the server to send me back a list of all the folders in that directory... but the problem is I can't seem to figure out how to update my backbone collection with the new data returned by the server when i do this.model.save()... I see in my web inspector that the response is being sent, so how do I update a collection; flushing out all the old data and updating it with the new data that was returned by the server?

any help with this would be greatly appreciated.

AlexW.H.B.
  • 1,781
  • 3
  • 25
  • 50
  • How are you using `save` to do this? I'd think you might use `collection.fetch` (http://backbonejs.org/#Collection-fetch). – WiredPrairie Apr 17 '13 at 01:15
  • @WiredPrairie I'm originally using collection.fetch to get the first list of directories, then i send my server the directory the user clicked on using post, because Get does not allow me to send file paths... then I get a response from the server with a new list of directories with this.model.save(), but i cannot seem to figure out how to access them. – AlexW.H.B. Apr 17 '13 at 01:27

1 Answers1

1

You are meaning collection.fetch. To do the server call:

this.collection.fetch();

To listen in the view the change:

this.collection.on('add', this.newModel);

Also in previous bakcbone version you could listen to the reset event instead of add. You could get more info about usage here: http://backbonejs.org/#Collection-fetch

Daniel Aranda
  • 6,426
  • 2
  • 21
  • 28
  • does this work to get info from a post request? the problem is I'm using the slim framework to pass info to my server side code, and it does not allow me to use get to pass file paths, so I have to use post... and my post go through correctly, and return, but I can't seem to access the data that it returned in my backbone app – AlexW.H.B. Apr 17 '13 at 03:05
  • 1
    fetch "extends" the $.ajax call, so you could do something like this.collection.fetch({type:"POST"}); to do the post Request. – Daniel Aranda Apr 17 '13 at 03:10