0

下面的 ember 示例使用 Foundationcss 显示手风琴。我已经使用 ember 集合视图实现了手风琴,并通过内容绑定将数据传递给它。

如何在集合视图中的每个项目渲染中显示来自 Fixtures 的 NAME 和 DESC?

请使用我的 jsFiddle 来理解我的问题:http: //jsfiddle.net/theremin/6hLbC/

模板

<script>
  $(document).foundation();
</script>
<script type="text/x-handlebars" data-template-name="application">
    <h1>Example</h1>
    {{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="index">
    {{view App.AccordionView contentBinding="content"}}
</script>

JS

App = Ember.Application.create({});

App.Store = DS.Store.extend({
    revision    : 12,
    adapter     : 'DS.FixtureAdapter'
});

App.Router.map( function() {

});

App.IndexRoute = Ember.Route.extend({
    model : function(){
        return App.Device.find();
    }
})

App.AccordionController = Ember.ArrayController.extend({

});

App.AccordionView = Ember.CollectionView.extend({
    tagName : "div",
    classNames : ["section-container", "accordion"],
    attributeBindings : ["data-section"],
    "data-section" : "accordion",
    itemViewClass     : Ember.ContainerView.extend({
        tagName : "section",
        childViews          : ["titleView", "contentView"],
        titleView    : Ember.View.extend({
            tagName   : "div",
            classNames : ["title"],
            template  : Ember.Handlebars.compile('<p><a href="#">Name:{{name}}{{content}}</a></p>')
        }),
        contentView  : Ember.View.extend({
            tagName   : "div data-section-content",
            classNames : ["content"],
            template  : Ember.Handlebars.compile('<p>desc:{{desc}}</p>')
        }),
    }),
})



App.Device = DS.Model.extend({
    name : DS.attr('string'),
    desc : DS.attr('string')
});
App.Device.FIXTURES = [{
    id : 1,
    name: "Plug1",
    desc: "Some words about plug1"
},
{
    id : 2,
    name: "Plug2",
    desc: "Some comments about plug2"    
}
];
4

1 回答 1

1

工作版本在这里http://jsfiddle.net/6hLbC/1/

基本上,Ember 在您制作自定义 ContainerView 时不会自动继承上下文,因此您需要专门定义它。

itemViewClass     : Ember.ContainerView.extend({
    tagName : "section",
    childViews          : ["titleView", "contentView"],
    titleView    : Ember.View.extend({
        tagName   : "div",
        classNames : ["title"],
        contextBinding: "parentView.content",
        template  : Ember.Handlebars.compile('<p><a href="#">Name:{{name}}{{content}}</a></p>')
    }),
    contentView  : Ember.View.extend({
        tagName   : "div data-section-content",
        classNames : ["content"],
        contextBinding: "parentView.content",
        template  : Ember.Handlebars.compile('<p>desc:{{desc}}</p>')
    }),
}),
于 2013-04-13T03:54:08.070 回答