有没有办法将bindData传递给Marionette的bindTo,类似于jQuery的绑定?木偶传递bindData与bindTo
我读了jQuery的网站上,你可以做以下的传递bindData:
function myFunction(event){
console.log(event.data.foo); // output "bar"
};
$(".some-element").bind("click", {foo:"bar"}, myFunction);
我想这样做的原因是因为我结合多种功能于一个途径。
第一个函数只是使用路由中的参数,没什么特别的。
第二功能需要被传递给它的自定义数据,这是在bindData用武之地。
控制器和路由器
var Controller = {
page1: function(itemId){
Vent.trigger('openPageOne', itemId);
}
};
var AppRouter = Marionette.AppRouter.extend({
controller: Controller,
appRoutes: {
"page1/:itemid" : "page1"
},
start: function() {
Backbone.history.start();
}
});
首先绑定
这工作很好地,并且在导航到该路线时,我会得到打印在控制台上的itemId。
var MyLayout = Marionette.Layout.extend({
initialize: function(){
_.bindAll(this);
},
myFunction: function(itemId){
console.log(itemId);
}
});
var myLayout = new MyLayout();
myLayout.bindTo(Vent, "openPageOne", myLayout.myFunction);
第二个绑定
这是我失败:(
我想自定义的数据对象传递给函数。
里面anotherFunction,我想显示foo的值。
var AnotherLayout = Marionette.Layout.extend({
initialize: function(){
_.bindAll(this);
},
anotherFunction: function(event){
// Here is where I want to use foo
console.log(event.data.foo);
}
});
var anotherLayout = new AnotherLayout();
anotherLayout.bindTo(Vent, "openPageOne", {foo:"bar"}, anotherLayout.anotherFunction);
更具体而言,第一功能应该改变我的网页的内容。第二个功能应突出显示项目我的导航菜单。我想发送给我的函数的自定义对象基本上是菜单项的元素ID,所以我可以添加一个类到它。
我刚刚接近这个错误的方式吗?任何输入都会有帮助。谢谢!
Kalpers:谢谢你的回复。这种方法将起作用。的确,直接绑定到Vent是一种传递触发时间数据的方法。然而,据我的理解,如果你直接绑定到通风口,你会在视图被杀后触发僵尸事件触发风险(如果我错了,请纠正我)。这就是为什么我试图做View.bindTo(现在View.listenTo),但我想我们可以绕过这个问题,如果我们手动解除绑定?:/ Bah也许我不知道我在说什么。 – Jupo 2013-03-26 05:39:01
我还没有看到用于发泄的ListenTo,但我有一个由Marionette解雇的onClose方法,我删除了绑定。我真的很喜欢木偶。你真的必须调试代码才能真正了解它在做什么。当我遇到这些信息时,我会更新这条评论。感谢您提出这个问题。 – Kalpers 2013-03-26 11:36:36