Skip to content Skip to sidebar Skip to footer

Alert Window In A Different View After Form Submit Using Backbone.js

Using backbone.js, and I have a form that I am able to submit and after submitting, the savepost() function redirects it to the dashboard url. The dashboard is a different view, an

Solution 1:

Sounds like you need a global event bus.

Here's a great example jsfiddle, author is credited in the fiddle

https://jsfiddle.net/JamesOR/m8J9L/

var eventBus = _.extend({}, Backbone.Events);

Then, inside the dashboard view, listen for the event. In the fiddle you'll notice the author uses .on. This is old style, the backbone author now recommends listenTo, because once the view is removed, all listeners are removed automatically. This is not the case with on

initialize: function () {
    this.listenTo(eventBus,'formSubmitted',this.onFormSubmit);
}

Then, when the form is submitted, trigger the event.

eventBus.trigger('formSubmitted', /** data that you want to send to handler*/);

In the handler method

onFormSubmit:function(/** data that sent to this handler*/){
    //code here
}

Post a Comment for "Alert Window In A Different View After Form Submit Using Backbone.js"