查找Backbone.js的查看,如果你知道产品型号? [英] Find a Backbone.js View if you know the Model?

查看:116
本文介绍了查找Backbone.js的查看,如果你知道产品型号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,创建一个< UL> ),它创建子视图( RowView ,创建<李> )的集合中的每个型号,我有一个问题,在收集设置内联编辑这些模型。

Given a page that uses Backbone.js to have a Collection tied to a View (RowsView, creates a <ul>) which creates sub Views (RowView, creates <li>) for each Model in the collection, I've got an issue setting up inline editing for those models in the collection.

我创建的,它取代了里的 RowView 视图中的编辑()方法与文本框的内容,如果用户presses 设置页而在该文本框,我想触发列表中的下一个视图的编辑()方法。

I created an edit() method on the RowView view that replaces the li contents with a text box, and if the user presses tab while in that text box, I'd like to trigger the edit() method of the next View in the list.

我可以得到集合中的下一个模型的模型:

I can get the model of the next model in the collection:

// within a RowView 'keydown' event handler
var myIndex = this.model.collection.indexOf(this.model);
var nextModel = this.model.collection.at(myIndex+1);

但问题是,如何找到一个连接到模型视图。父 RowsView 查看不保留所有的孩子们意见的参考;这是渲染()的方法就是:

But the question is, how to find the View that is attached to that Model. The parent RowsView View doesn't keep a reference to all the children Views; it's render() method is just:

this.$el.html(''); // Clear
this.model.each(function (model) {
    this.$el.append(new RowView({ model:model} ).render().el);
}, this);

我是否需要重写一遍,以保持指针的一个单独的数组的所有 RowView s具有下呢?或者是有找到查看一个聪明的办法那种有连接到它已知的型号?

Do I need to rewrite it to keep a separate array of pointers to all the RowViews it has under it? Or is there a clever way to find the View that's got a known Model attached to it?

下面是整个问题的的jsfiddle:<一href=\"http://jsfiddle.net/midnightlightning/G4NeJ/\">http://jsfiddle.net/midnightlightning/G4NeJ/

Here's a jsFiddle of the whole problem: http://jsfiddle.net/midnightlightning/G4NeJ/

推荐答案

这是不优雅,以存储在模型视图的引用,但是你可以链接查看与事件模型,做到这一点:

It is not elegant to store a reference to the View in your model, however you could link a View with a Model with events, do this:

// within a RowView 'keydown' event handler
var myIndex = this.model.collection.indexOf(this.model);
var nextModel = this.model.collection.at(myIndex+1);
nextModel.trigger('prepareEdit');

在RowView监听事件prepareEdit并在监听通话编辑(),像这样:

In RowView listen to the event prepareEdit and in that listener call edit(), something like this:

this.model.on('prepareEdit', this.edit);

这篇关于查找Backbone.js的查看,如果你知道产品型号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆