(BackboneJS) Run a View after return from the server

1

In my application I have 2 Views .

One of them is sending the data to the server when the client clicks on submit .

The other needs to wait for this response and run to work with the 'upgraded' data.

How would I do such a thing?

    
asked by anonymous 18.08.2015 / 20:02

1 answer

0

The easiest way would be to use the native features of the backbone collection.

  • Replace the input type submit with an input type button (or a div) so that you do not have to cancel the default behavior of the HTML element.
  • Option 1:

  • Perform insertion (POST) via collection.

    • In view 1 create a model with the values that would be submitted by the form.
    • Add the created model to the collection with 'create' to add and request the server by sending the new model ( backbone-collection -create ).
  • In view 2 use a new collection pointing to the same url, and listen for the add event to add the new item inserted in the collection (server) using fetch with the remove option as false if you do not want the entire collection to be upgraded: ( backbone-collection-fetch )

    collection.fetch({remove: false});

  • Option 2:

  • View via JSON (or JSONP in the case of cross domain);
  • In view 2 :

    • Listen for the add event in the same way as the example above.
    • Or perform a backbone-trigger trigger from view 1 to view 2 by passing the new model and manually add to the view the trigger triggered event and perform an append of the HTML with the new model data that was passed via trigger).
  • 20.08.2015 / 19:42