(Backbonejs) Run a View after server return

Asked

Viewed 47 times

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 that reply and run to work with the 'updated' data'.

How would I do such a thing?

  • When the first view is submitted do you make an ajax request? You can’t do the second view action in the callback of that request?

1 answer

0

The easiest way would be to utilize the backbone Collection’s native features.

  1. Replace the input type Submit with an input type button (or a div) so that you don’t have to cancel the default behavior of the HTML element.

Option 1:

  1. Perform insertion (POST) via Collection.

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

    collection.fetch({remove: false});

Option 2:

  1. In the view 1 perform insertion (POST) via JSON (or JSONP in the case of cross Omain);
  2. In the view 2:

    • Listen to the add event in the same way as the example above.
    • Or perform a Rigger (bone-Trigger) of view 1 to the view 2 passing the new model and manually adding to the view (listen in view 1 the event triggered by Trigger and perform an HTML append with the data of the new model that was passed via Trigger).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.