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 badges
Add a ment  | 

4 Answers 4

Reset to default 5

The 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