gpt4 book ai didi

backbone.js - 为什么行为不允许动态传递?

转载 作者:行者123 更新时间:2023-12-05 00:26:02 25 4
gpt4 key购买 nike

我正在研究 Marionette.behavior。我试图在 View 初始化时动态传递行为哈希,但它没有被分配给 View 的行为对象。因为行为在 View 构建时被初始化。
所以我们通过以下方式实现了解决方案,但这是实现它的正确方法吗?
有没有其他方法可以实现?和
为什么行为不允许动态传递?

这是代码:

var Behaviour = new Marionette.Application();

Behaviour.addRegions({
mainRegion:"#main-region"
});


var Person = Backbone.Model.extend({
defaults:{
firstName:"NA",
lastName:"NA",
phoneNumber:"NA",
presentAddr:"NA",
permanantAddr:"NA"
}
});


var buttonView=Marionette.ItemView.extend({

template:"#buttontemplate",


constructor:function(options){


this.behaviors = options.behaviors;
Marionette.ItemView.apply(this, arguments);

},


events:{

"click .display":"displayDetail"
},

displayDetail:function(){

this.triggerMethod("DisplayPersonDetails");

},

//behaviors:{Behavior1:{ },Behavior2:{ }}
})


var PersonDetailsView = Marionette.ItemView.extend({

template:"#static-template",

ui: {
"Change": ".change"
},

events:{

"click @ui.Change":"changeBehavior"
},

changeBehavior:function(){

},

});

var Behavior1 = Marionette.Behavior.extend({

onDisplayPersonDetails:function(){

var person=new Person({firstName:"abhijeet",lastName:"avhad",phoneNumber:"9604074690",permanantAddr:"sangamner",presentAddr:""})
var myView = new PersonDetailsView({model:person});
Behaviour.mainRegion.show(myView);

}

});

var Behavior2 = Marionette.Behavior.extend({

onDisplayPersonDetails:function(){


var person =new Person({firstName:"abhijeet",lastName:"avhad",phoneNumber:"9604074690",permanantAddr:"",presentAddr:"shivajinagar"})
var myView =new PersonDetailsView({model:person});
Behaviour.mainRegion.show(myView);


}

});


Behaviour.on("initialize:after", function(){

console.log(" started!");

Marionette.Behaviors.behaviorsLookup = function() {
return window.Behaviors;
};


window.Behaviors = {};

window.Behaviors.Behavior1 = Behavior1;
window.Behaviors.Behavior2 = Behavior2;

var buttonview=new buttonView({behaviors:{Behavior1:{ },Behavior2:{}}});
Behaviour.mainRegion.show(buttonview);

});
Behaviour.start();

最佳答案

实现这一点的另一种方法是在您的定义中声明一个函数,该函数返回初始化时提供的行为,如下所示:

var buttonView=Marionette.ItemView.extend({
...
behaviors: function () {
return this.options.behaviors;
},
...

关于backbone.js - 为什么行为不允许动态传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23062530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com