简体   繁体   English

访问视图中的元素-Backbone.JS

[英]Accessing elements inside a view - Backbone.JS


EDIT : SOLVED 编辑:已解决

Two typos in the code, one as stated and another sneaky one, Thanks alot and sorry for your time! 代码中有两个错字,一个错字,另一个错字,非常感谢,很抱歉!

Hello SO, 你好,

My current issue occurred while exploring Backbone.js 我当前的问题是在浏览Backbone.js时发生的

I've created a simple HTML table using backbone, Each row is a model, The collection of all the rows is passed to a view that renders everything - so far, so good. 我使用主干创建了一个简单的HTML表,每一行都是一个模型,所有行的集合都传递给一个呈现所有内容的视图-到目前为止,一切都很好。

My table has the following classes : Each tablecell has a .tablecell class and each row has a .tablerow class. 我的表具有以下类:每个表单元格都有一个.tablecell类,每行都有一个.tablerow类。

I currently have a click event that fires correctly every time a table cell is clicked. 我目前有一个单击事件,每次单击表格单元格时都会正确触发。 What I'd like to figure out is how, if at all possible, can I access the 'clicked on' table cell and alter his attributes. 我想弄清楚的是,如果可能的话,我该如何访问“已单击”表格单元并更改其属性。 As a simple example, Change the class of a clicked cell or change the background color. 作为一个简单的示例,更改单击的单元格的类或更改背景颜色。

Can it even be done? 还能做到吗? Or am I missing something completely? 还是我完全错过了什么?

My current thought is that each cell must be a model of his own with his own view and his own event, but I doubt it should be so complicated. 我目前的想法是,每个单元都必须是自己的模型,具有自己的观点和事件,但是我怀疑它是否会如此复杂。 Thanks in advance! 提前致谢!

My single view is this : 我的唯一观点是这样的:

            var rowView = Backbone.View.extend({

                //usual view attributes : el, model, initialize etc.

                events : 
                    {
                    "click.tablecell" : "clickOnCell",
                    },

                    clickOnCell : function(cell)
                    {
                        cell = $(cell.currentTarget);
                        cell.css("font-weight","bold");
                    },
                render : function () {

        // boring render method, renders correctly.

                },  
            });

You must add the event and handler to the events hash (you are missing a blankspace on your example): 您必须将事件和处理程序添加到events哈希中(示例中缺少空格):

events: { 'click .tablecell': 'clickOnCell' }

You receive the jQuery (or another framework you are replacing jQuery with) event object: 您收到jQuery(或您要替换为jQuery的另一个框架)事件对象:

clickOnCell: function (e) {

    var $td = $(e.currentTarget);

    $td.css('color', 'red');

}

Understand that event delegation occurs at the end of view instantiation . 了解事件委派发生在视图实例化的末尾。 By that, I mean that your view must have a the el property as your row. 那样的话,我的意思是您的视图必须具有el属性作为行。

If you cannot set el property during the view definition (which seems to be the case), try using setElement to your table row element during the render . 如果无法在视图定义期间设置el属性(似乎是这种情况),请尝试在render期间将setElement表行元素。

render: function () { 

   //...

   var myRow = $('<tr/>');

   this.setElement(myRow);        

   //...

}

Hope I've helped. 希望我有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM