English 中文(简体)
如何在 emberjs 中从它的视图中操作列表项目
原标题:How to manipulate a list item from it s view in emberjs
  • 时间:2012-05-22 20:24:12
  •  标签:
  • ember.js

我有一个有视图和控制器的 Ember 应用程序:

http://jsfidle.net/gavriguy/EDr4G/" rel="nofollow">http://jsfidle.net/gavriguy/EDr4G/

I want to mark the current item the user clicks as read - by changing it s related model. I m currently able to do that by figuring the item s index of the view - but the problem is that i can t be sure that the index on the view is the same as the index on its controller. Any thoughts?

<% 1 > JavaScript :

App.tempController = Em.ArrayController.create({
    content: [
        {
        title:  A ,
        unread: true},
    {
        title:  B ,
        unread: true},
    {
        title:  C ,
        unread: false}
    ]
});

App.someItemsView = Ember.CollectionView.create({
    contentBinding:  App.tempController.content ,
    itemViewClass: Ember.View.extend({
        template: Ember.Handlebars.compile( <div>{{content.title}} unread: {{content.unread}}</div> ),
        click: function(event) {
            //How to mark current clicked item as read?
            console.log(this.content);
            console.log(event);
            this.set( content.unread , false);
        }
    })
});​
最佳回答

在您的 点击 hick 处理器中, 您可以通过 this.get( 内容) 获得查看所在的数组项目的引用。 所以您可以通过 this. setPath( 内容. unread, 假) 设置旗帜, 见 < a href=> http://jsfidle. net/pangratz666/ t6Nst/" rel="nofolpol" http://jsfidle. net/pangratz666/ t6Nst/ < /a> :

itemViewClass: Ember.View.extend({
    template: Ember.Handlebars.compile( <div>{{content.title}} unread: {{content.unread}}</div> ),
    click: function(event) {
        // this.content is the item in the array on which this click event occures.
        this.setPath( content.unread , false);
    }
})
问题回答

暂无回答




相关问题
Are Recursive Collections possible in sproutcore2?

I have a customizable navigation tree that can be nested 3 levels deep. Templates: <script type="text/x-handlebars" data-template-name="NavItemView"> <a {{bindAttr href="href" class="...

ember.js widgets

I know that ember js is good for single page apps and it appears that you can localize ember js app to a single dom container rather than the whole page, so I m wondering if ember js would be a good ...

Ember.js binding models stored within an array

What is the correct way to bind from one model to another when the models are stored within an array? Typically I d imagine that this would be a Controller s content array, but to keep the example ...

rendering views dynamically on EmberJS

I just having a little trouble to implement a special kind of view for Ember, I m digging on the source for days but can t find how to make it work... Can you take a look and tell me what s wrong? It ...

热门标签