在不同的视图警告窗口形式提交后Backbone.js的使用 [英] alert window in a different view after form submit using backbone.js
问题描述
使用Backbone.js的,我有,我可以提交并提交之后,savepost()函数,它重定向到URL仪表盘形式。仪表板是一个不同的看法,并希望该网页说您的文章被提交。我可以使用本地存储,但不希望依赖于它。
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, and would like that page to say "your post was submitted". I could use localstorage, but do not want to depend on it.
我也看到了流行的<一个href=\"http://stackoverflow.com/questions/20205707/passing-data-from-one-backbone-view-to-another/20358509#20358509\">event聚合选项,但不知道如果那样的看法在不同的页面被渲染工作。
I also see the popular event aggregator options, but not sure if that would work on views being rendered in the a different page.
推荐答案
听起来像是你需要一个全球性的事件总线。
Sounds like you need a global event bus.
下面是一个很好的例子的jsfiddle,笔者在小提琴记
Here's a great example jsfiddle, author is credited in the fiddle
https://jsfiddle.net/JamesOR/m8J9L/
var eventBus = _.extend({}, Backbone.Events);
然后,仪表板视图内,监听的事件。在拨弄你会发现笔者使用。对
。这是旧的风格,笔者骨干现在建议 listenTo
,因为一旦视图被删除,所有听众将自动删除。这不是与的情况下
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*/);
在处理方法
onFormSubmit:function(/** data that sent to this handler*/){
//code here
}
这篇关于在不同的视图警告窗口形式提交后Backbone.js的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!