admin管理员组文章数量:1306926
I am trying to trigger the event of parent view from its child view. But it seems that the parent view's event did not gets triggered.
Following is the sample for my code:
<!DOCTYPE html>
<html>
<head>
<script src=".4.4/jquery.min.js"></script>
<script src=".js/1.1.4/underscore-min.js"></script>
<script src=".js/0.3.3/backbone-min.js"></script>
<script>
MySubView = Backbone.View.extend({
id : "MySubView",
initialize: function() {
console.log("pro1");
this.trigger("testGo", "test");
}
});
MyView = Backbone.View.extend({
id : "MyView",
initialize: function() {
console.log("pro");
this.subview = new MySubView();
this.subview.listenTo("testGo", this.addFoo, this);
},
addFoo: function() {
console.log("ok");
alert("ok");
}
});
new MyView();
</script>
</head>
<body>
</body>
</html>
I tried to get hint from many solutions found via google search, but it seems I am got struck somewhere. Some of the options which I found are :
1/ How to trigger an event from child view to parent view in backbonejs and requirejs
2/ Backbone: How to trigger methods in a parent view
I am trying to trigger the event of parent view from its child view. But it seems that the parent view's event did not gets triggered.
Following is the sample for my code:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis./ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://ajax.cdnjs./ajax/libs/underscore.js/1.1.4/underscore-min.js"></script>
<script src="http://ajax.cdnjs./ajax/libs/backbone.js/0.3.3/backbone-min.js"></script>
<script>
MySubView = Backbone.View.extend({
id : "MySubView",
initialize: function() {
console.log("pro1");
this.trigger("testGo", "test");
}
});
MyView = Backbone.View.extend({
id : "MyView",
initialize: function() {
console.log("pro");
this.subview = new MySubView();
this.subview.listenTo("testGo", this.addFoo, this);
},
addFoo: function() {
console.log("ok");
alert("ok");
}
});
new MyView();
</script>
</head>
<body>
</body>
</html>
I tried to get hint from many solutions found via google search, but it seems I am got struck somewhere. Some of the options which I found are :
1/ How to trigger an event from child view to parent view in backbonejs and requirejs
2/ Backbone: How to trigger methods in a parent view
Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked Aug 26, 2014 at 17:26 turtleturtle 1,6191 gold badge14 silver badges30 bronze badges4 Answers
Reset to default 5The problem is that you are using listenTo wrong.
anObject.listenTo(anotherObject, 'forSomeEvent', function () { console.log('do something'); });
So, in your case, you should do it like this:
MyView = Backbone.View.extend({
id : "MyView",
initialize: function() {
console.log("pro");
this.subview = new MySubView();
this.listenTo(this.subview, 'testGo', this.addFoo);
},
addFoo: function() {
console.log("ok");
alert("ok");
}
});
Hope it helps!
Your listenTo
usage is slightly off
The signature is object.listenTo(otherObject, event, callback)
so you want something like:
this.listenTo(this.subview, "testGo", this.addFoo);
Which tells this
to listen to this.subview
for the event testGo
and call this.addFoo
when the event is triggered
try this
this.listenTo(this.subview, "testGo",this.addFoo);
signature:
object.listenTo(other, event, callback)
Triggering event:
Backbone.Events.trigger('<eventname>', {data1 to be passed with event}, {data2 to be passed with event}...);
Listener:
Backbone.Events.bind('<eventname>', callback, context);
本文标签: javascriptBackboneTrigger the parent view event from child viewStack Overflow
版权声明:本文标题:javascript - Backbone - Trigger the parent view event from child view - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741827080a2399709.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论