在 Backbone View 中触发 el 事件

发布于 2024-12-13 11:33:14 字数 1982 浏览 0 评论 0原文

我有一个创建并填充选择列表的视图。我想在 Change 事件上绑定一个事件(当用户选择不同的选项时。

它只是输出与此接近的内容:

<select>
   <option value="id">ID Name</option
</select>

视图:

App.Views.SessionList = Backbone.View.extend({
   tagName: 'select',
   id: 'sessionList',
   events: {
       'change': 'selectionChanged'
   },
   initialize: function () {
       // Load Collection
       _.bindAll(this, 'selectionChanged');

       this.template = _.template($('#optionsTemplate').html());
       this.Sessiontemplate = _.template($('#session_template').html());
       this.SelectedSessiontemplate = _.template($('#selected_session_template').html());

       // Create Collection
       this.SessionList = new App.Collections.SessionList();

       // Set Bindings
       this.SessionList.bind('add', this.addSession, this);
       this.SessionList.bind('reset', this.addSessions, this);


       this.render();

       this.SessionList.fetch();
   },
   render: function () {

       return this;
   },
   addSession: function (item, num) {
       if (num === 0) {
           $(this.el).append(this.SelectedSessiontemplate(item.toJSON()));
           console.log("Selected");
       }
       else {
           $(this.el).append(this.Sessiontemplate(item.toJSON()));
       }
   },
   addSessions: function () {
       var self = this;
       // Add all Rows
       for (var i = 0; i < self.SessionList.models.length; i++) {
           this.addSession(self.SessionList.models[i], i);
       }
   },
   selectionChanged: function (e) {
       var field = $(e.currentTarget);
       var value = $("option:selected", field).val();
   }
});

会话模板只是简单:

<option value="{{Id}}">{{Name}}</option>

该事件永远不会被触发,而且似乎它不是'我想在选择列表发生变化时触发它,

我最初认为我可能遇到类似的问题: backbone.js 事件和 el,但是在这种情况下不起作用。

I have a view that creates and populates a Selectlist. I want to bind an event on the Change event (When the user selects a different Option.

It simply outputs something close to this:

<select>
   <option value="id">ID Name</option
</select>

The View:

App.Views.SessionList = Backbone.View.extend({
   tagName: 'select',
   id: 'sessionList',
   events: {
       'change': 'selectionChanged'
   },
   initialize: function () {
       // Load Collection
       _.bindAll(this, 'selectionChanged');

       this.template = _.template($('#optionsTemplate').html());
       this.Sessiontemplate = _.template($('#session_template').html());
       this.SelectedSessiontemplate = _.template($('#selected_session_template').html());

       // Create Collection
       this.SessionList = new App.Collections.SessionList();

       // Set Bindings
       this.SessionList.bind('add', this.addSession, this);
       this.SessionList.bind('reset', this.addSessions, this);


       this.render();

       this.SessionList.fetch();
   },
   render: function () {

       return this;
   },
   addSession: function (item, num) {
       if (num === 0) {
           $(this.el).append(this.SelectedSessiontemplate(item.toJSON()));
           console.log("Selected");
       }
       else {
           $(this.el).append(this.Sessiontemplate(item.toJSON()));
       }
   },
   addSessions: function () {
       var self = this;
       // Add all Rows
       for (var i = 0; i < self.SessionList.models.length; i++) {
           this.addSession(self.SessionList.models[i], i);
       }
   },
   selectionChanged: function (e) {
       var field = $(e.currentTarget);
       var value = $("option:selected", field).val();
   }
});

The Session Template is just simply:

<option value="{{Id}}">{{Name}}</option>

The event never gets triggered, and It seems that it isn't getting binded correctly. I want to trigger it on the change of the select list.

I originally thought I may be having an issue similar to:
backbone.js events and el, however it doesn't work in this case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱没勇气 2024-12-20 11:33:14

直接调试您的问题很困难,因为我们没有所有信息(SessionList 看起来像什么......模板等)。

但是,我已将您的示例与事件确实有效的一些代码配对。希望你能从那里建立它?如果您想到达一个对您来说失败的地方,您可以fork jsFiddle,我们可以尝试进一步提供帮助。

window.App = { Views: {} };

App.Views.SessionList = Backbone.View.extend({
    tagName: 'select',

    events: {
        'change': 'selectionChanged'
    },

    initialize: function () {
        _.bindAll(this, 'selectionChanged');
        this.render();
    },

    render: function () {
        $(this.el).append('<option value="foo">Option 1</option>');
        $(this.el).append('<option value="bar">Option 2</option>');                
        return this;
    },

    selectionChanged: function (e) {
        var value = $(e.currentTarget).val();
        console.log("SELECTED", value);
    }
});

var view = new App.Views.SessionList();
$("body").append(view.el);

有了该代码,每次选择一个项目时,您都会获得一个控制台日志,其中包含该值。

既然你没有明白这一点,我猜你看到了异常或类似的情况?你的调试器会给你任何提示吗?

祝你好运!

It is hard to debug your problem directly, because we don't have all of the information (what does SessionList look like... the templates, etc).

BUT, I have paired down your example to some code where the event does, indeed work. Hopefully, you can build it up from there? You can fork the jsFiddle if you want to get to a place where it fails for you and we can try to help further.

window.App = { Views: {} };

App.Views.SessionList = Backbone.View.extend({
    tagName: 'select',

    events: {
        'change': 'selectionChanged'
    },

    initialize: function () {
        _.bindAll(this, 'selectionChanged');
        this.render();
    },

    render: function () {
        $(this.el).append('<option value="foo">Option 1</option>');
        $(this.el).append('<option value="bar">Option 2</option>');                
        return this;
    },

    selectionChanged: function (e) {
        var value = $(e.currentTarget).val();
        console.log("SELECTED", value);
    }
});

var view = new App.Views.SessionList();
$("body").append(view.el);

Given that code, you will get a console log with the value every time you select an item.

Since you aren't getting that, I am guessing you are seeing an exception or something like that? Does your debugger give you any hints?

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文