简体   繁体   English

模板中的Ember.js属性和ArrayController

[英]Ember.js property and ArrayController in template

I've got a setup like this in Ember: 我在Ember中有这样的设置:


App.ListObject = Ember.Object.create({
        knownThings: function() {
                var ot = this.openThings.get('content');
                var ct = this.closedThings.get('content');
                var kt = ot.concat(ct);
                var known = Ember.ArrayController.create({content: kt});
                return known;
                }.property(),

    openThings: Ember.ArrayController.create({
        content: []
        }), 

    closedThings: Ember.ArrayController.create({ 
        content: []
    }), 
})

Basically, known things is the combined arrays of openThings and closedThings. 基本上,已知的东西是openThings和closedThings的组合数组。 I can't seem to figure out how to iterate over knownThings in the template. 我似乎无法弄清楚如何遍历模板中的knownThings。 Just doing 只是做

 {{#each App.ListObject.knownThings }} 

Does not work as the property needs to be accessed like App.ListObject.get('knownThings') but that doesn't work in the template unless I'm doing something terribly wrong. 不能正常工作,因为该属性需要像App.ListObject.get('knownThings')一样进行访问,但是除非我做错了什么,否则它在模板中不起作用。 Iterating over the other attributes in the template does work (open and closed things) 遍历模板中的其他属性确实起作用(打开和关闭的东西)

So, how would you iterate over knownThings in the template? 那么,您将如何遍历模板中的knownThings?

Slight Modifications needed... 需要稍作修改...

Firstly, 首先,

knownThings: function() {
  //use get to retrieve properties in ember, Always !
  var ot = this.get('openThings').get('content');
  //var ot = this.get('openThings.content') if you are using latest ember
  var ct = this.get('closedThings').get('content');
  //var ot = this.get('closedThings.content') if you are using latest ember
  var kt = ot.concat(ct);
  var known = Ember.ArrayController.create({content: kt});
  return known;
  //Add dependencies to keep your knownThings in sync with openThings & closedThings if at all they change in future
}.property('openThings', 'closedThings')

Coming to Handlebars iterate using 来到车把使用

//you forgot content property, and in handlebars you don;t need to use get, dot operator is enough
{{#each App.List.knownThings}}

Let me know if this works... 让我知道这个是否奏效...

Update Working Fiddle ... 更新工作提琴 ...

Unless I didn't understand what you're saying, I think you should have ListObject extending Em.ArrayController instead of Em.Object . 除非我不明白您在说什么,否则我认为您应该让ListObject扩展Em.ArrayController而不是Em.Object Also, if your property depends on content , it should be .property('content.@each') . 另外,如果您的财产取决于content ,则应为.property('content.@each') If you're using the router, your template should look like {{#each thing in controller.knownThings}} and you use {{thin.something}} , if not using router, then {{#each item in App.listObject.knownThings}} . 如果您使用的是路由器,则模板应类似于{{#each thing in controller.knownThings}}并且您使用{{thin.something}} ,如果未使用路由器,则{{#each item in App.listObject.knownThings}} Also, openThings and closedThings don't seem to be correct and the way you're accessing them is wrong too. 另外, openThingsclosedThings似乎也不正确,并且您访问它们的方式也是错误的。

I didn't write a fiddle for this specific case cause I don't really know what you're trying to do, but take a look at this fiddle , specifically at App.ResourcesController and the template 'resources-view': 我没有为这种特殊情况写小提琴,因为我真的不知道您要做什么,但是请看一下这个小提琴 ,特别是App.ResourcesController和模板“ resources-view”:

Controller: 控制器:

// ...
App.ResourcesController = Em.ArrayController.extend({
    content: [],
    categories: ['All', 'Handlebars', 'Ember', 'Ember Data', 'Bootstrap', 'Other'],
    categorySelected: 'All',
    filtered: function() {
        if(this.get('categorySelected') == "All") {
            return this.get('content');                                        
        } else {
            return this.get("content")
                       .filterProperty(
                           "category",
                           this.get('categorySelected')
                       );
        }            
    }.property('content.@each', 'categorySelected'),
    filteredCount: function() {
        return this.get('filtered').length;                                        
    }.property('content.@each', 'categorySelected'),
    hasItems: function() {
        return this.get('filtered').length > 0;
    }.property('filteredCount') 
);
// ...

Template: 模板:

<script type="text/x-handlebars" data-template-name="resources-view">
    <h1>Ember Resources</h1>
    {{#view Bootstrap.Well}}
        The following is a list of links to Articles, Blogs, Examples and other types of resources about Ember.js and its eco-system.
    {{/view }}

    {{view Bootstrap.Pills contentBinding="controller.controllers.resourcesController.categories" selectionBinding="controller.controllers.resourcesController.categorySelected"}}
    <i>{{controller.filteredCount}} Item(s) Found</i>
    {{#if controller.hasItems}}
    <ul>
    {{#each resource in controller.filtered}}
        <li>
            <a {{bindAttr href="resource.url" 
                          target="resource.target"
                          title="resource.description"}}>
               {{resource.htmlText}}
            </a>
        </li>
    {{/each}}        
    </ul>
    {{else}}
        {{#view Bootstrap.AlertMessage type="warning"}}
            Couldn't find items for {{controller.categorySelected}}
        {{/view}}
    {{/if}}
</script>

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

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