如何处理相似但不同视图的点击事件?

发布于 2024-11-02 21:58:15 字数 1360 浏览 1 评论 0原文

我正在尝试使用 Backbone.js 实现一个简单的工具栏。我有以下简单的主干代码:

var Toolbox = Backbone.View.extend({
    el: $("#toolbox ul"),

    initialize : function() {
        _.bindAll(this, "addOne");
        toolCollection.each(this.addOne);
    },

    addOne : function(tool) {
        var view = new ToolView({ model: tool });
        $(this.el).append(view.render().el);
    }
});

// Tool model and collections
var Tool = Backbone.Model.extend();

var toolCollection = new Backbone.Collection([
    new Tool({ 
        tool:   "toolName1"
    }),
    new Tool({ 
        tool:   "toolName2"
    })
]);

// The view of the individual tools
var ToolView = Backbone.View.extend({
    tagName: "li",
    template : _.template($("#tool-template").html()),

    events: {
        "click #toolbox ul li a": "toolClick"
    },

    initialize : function() {
        _.bindAll(this, "render", "toolClick");
        this.model.view = this;
    },

    render : function() {
        var mj = this.model.toJSON();
        $(this.el).html(this.template(mj));

        return this;
    },

    toolClick : function() {
        console.log("Tool clicked");
    }
});

var tb = new Toolbox;

因此,我有一个问题。显然我需要以不同的方式处理工具上的每次点击。 当我实例化视图时,我可以绑定特定的单击事件来处理该特定工具的单击吗?如果可以,我将在哪里编写单击事件?我什至不确定我是否在这里遗漏了一些东西,但是任何人都可以建议一种模式,告诉我如何拥有一组相关但不同的视图以及如何单独处理视图的点击?任何帮助将不胜感激。提前致谢。

I'm trying to implement a simple toolbar using Backbone.js. I have the following simple Backbone code:

var Toolbox = Backbone.View.extend({
    el: $("#toolbox ul"),

    initialize : function() {
        _.bindAll(this, "addOne");
        toolCollection.each(this.addOne);
    },

    addOne : function(tool) {
        var view = new ToolView({ model: tool });
        $(this.el).append(view.render().el);
    }
});

// Tool model and collections
var Tool = Backbone.Model.extend();

var toolCollection = new Backbone.Collection([
    new Tool({ 
        tool:   "toolName1"
    }),
    new Tool({ 
        tool:   "toolName2"
    })
]);

// The view of the individual tools
var ToolView = Backbone.View.extend({
    tagName: "li",
    template : _.template($("#tool-template").html()),

    events: {
        "click #toolbox ul li a": "toolClick"
    },

    initialize : function() {
        _.bindAll(this, "render", "toolClick");
        this.model.view = this;
    },

    render : function() {
        var mj = this.model.toJSON();
        $(this.el).html(this.template(mj));

        return this;
    },

    toolClick : function() {
        console.log("Tool clicked");
    }
});

var tb = new Toolbox;

So, with this, I have a question. I obviously need to handle each click on a tool differently.
When I instantiate my view, can I bind a specific click event to handle the click of that specific tool, and if so, where would I write the click event at? I'm not even sure if I'm missing something here, but could anyone suggest a pattern of how I can have a group of related, but different views and how to handle the click of view separately? Any help would be appreciated. Thanks in advance.

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

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

发布评论

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

评论(3

话少情深 2024-11-09 21:58:15

我希望我理解正确。

您有一个包含不同工具的工具箱。当然,您必须以不同的方式处理不同工具的点击。

那么,为什么不在 ToolBox 视图中包含所有单击事件并将 ID 附加到工具上呢?

events: {
        "click #toolbox #zoom": "zoomClick",
        "click #toolbox #pen": "penClick",
        "click #toolbox #line": "lineClick"
}

您可以在 ToolBox render() 函数中创建工具。希望有帮助。

I hope I understood you right.

You have a toolbox with different tools. And of course you have to handle clicks on different tools differently.

So, why don't you have all the click events in the ToolBox view with IDs attached to the tools.

events: {
        "click #toolbox #zoom": "zoomClick",
        "click #toolbox #pen": "penClick",
        "click #toolbox #line": "lineClick"
}

You can have the tools created in the ToolBox render() function. Hope that helps.

待天淡蓝洁白时 2024-11-09 21:58:15

另一种方法来处理这个问题。您已经捕获了对各个工具的点击并将其传递给 toolclick 函数。该函数知道所单击的模型,您只需将其传递给 switch 语句即可创建单独的行为。

toolClick: function() {
    var toolname = this.model.get("tool");
    switch(toolname) {
        case "toolName1":
            //Do something;
            break;
        case "toolName2":
            //Do something else;
            break;
    }
}

这样您就不必在渲染或模板中进行大量准备工作。

Another way to handle this. You are already capturing the click on the individual tools and passing it to the toolclick function. The function is aware of the model that was clicked you could just pass it to a switch statement to create your separate behavior.

toolClick: function() {
    var toolname = this.model.get("tool");
    switch(toolname) {
        case "toolName1":
            //Do something;
            break;
        case "toolName2":
            //Do something else;
            break;
    }
}

This way you don't have to do a bunch of prep in render or templates.

枕梦 2024-11-09 21:58:15

如果您的工具与其他工具相同但执行的操作不同,那么您需要通过扩展“普通”工具来创建该单独的工具。通过扩展,您可以添加新的属性和函数,也可以完全覆盖它们。

var ExtendedToolView = ToolView.extend({

    toolClick: function() {
        console.log("Extended Tool clicked");
    }

});

If your tool is the same but does something different than the other tools, then you need to create that separate tool by extending your "vanilla" tool. With extend you can either add new properties and functions or override them entirely.

var ExtendedToolView = ToolView.extend({

    toolClick: function() {
        console.log("Extended Tool clicked");
    }

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