简体   繁体   English

Ember.js ::无法在视图中呈现ember.js模型

[英]Ember.js :: Cannot render ember.js models in view

If anyone can put me out of my misery with this I would greatly appreciate it, drving me mad and I know it's gonna be something stupidly simple. 如果有人可以让我摆脱这种痛苦,我将不胜感激,让我发狂,我知道这将变得非常简单。

I have an array: 我有一个数组:

Data 数据

var test = [{"name":"Kober Ltd","town":"Bradford","type":"Z1CA","number":3650645629},
{"name":"Branston Ltd.","town":"Lincoln","type":"Z1CA","number":3650645630}]

and I want to render this info as child elements inside a collectionView: 我想将此信息呈现为collectionView中的子元素:

collectionView 的CollectionView

App.ThreeView = Ember.CollectionView.extend({
  itemViewClass: Ember.View.extend({    
      click: function(){
        alert('hello')
       },   
    classNames: ['element','foobar'],
    templateName: 'foo'
  })
})

and here is my controller: 这是我的控制器:

controller 调节器

App.ThreeController = Ember.ArrayController.extend({
content: [],
  init: function(){
    var me = this;
    $.each(test,function(k,v){
        var t = App.ThreeModel.create({
            name : v.name,
            town: v.town,
            type: v.type,
            number: v.number
        }) 
        me.addObject( t )
    })
    console.log( me.content )
  }
})

Templates: 模板:

    <script type="text/x-handlebars" data-template-name="application">
        {{outlet}}
    </script>

    <script type="text/x-handlebars" data-template-name="three">
    </script>

    <script type="text/x-handlebars" data-template-name="foo">
        <div class="symbol"> {{ view.content.type }} </div>
        <div class="number"> {{ view.content.number }} </div>
        <div class="name"> {{ view.content.name }} </div>
        <div class="town"> {{ view.content.town }} </div>
    </script>  

I am using the latest Ember so...V2 router that syncs up all the parts with the 'Three' name. 我正在使用最新的Ember so ... V2路由器,该路由器将所有零件同步为“ Three”名称。 Every will work if I put the array directly into the view: 如果我将数组直接放入视图中,每个方法都将起作用:

App.ThreeView = Ember.CollectionView.extend({
  content: test, // manually added a pure array into view content
  itemViewClass: Ember.View.extend({    
      click: function(){
          alert('hello')
      },    
      classNames: ['element','foobar'],
      templateName: 'foo'
  })
})

But when I try and do this 'properly', using Ember.js Objects, I get no rendered views ( aside from an empty application view ). 但是,当我尝试使用Ember.js对象“适当地”执行此操作时,没有任何渲染视图(除了空的应用程序视图之外)。

I have tried work arounds, like adding a 'contentBinding' from the view to the controller just to see if I can force a connection but still nothing. 我已经尝试了变通方法,例如从视图向控制器添加一个“ contentBinding”,只是看我是否可以强制连接,但仍然一无所获。 It is important that I render the view through the container as I am using Three.js to pick up on the rendered content and manipulate further. 在使用Three.js拾取呈现的内容并进行进一步操作时,请务必通过容器呈现视图。

So, to summarise: I can render pure arrays in view, but nothing passed from controller. 因此,总结一下:我可以在视图中渲染纯数组,但是没有从控制器传递任何东西。 Incidentally, the controller is definitely being instituted as I can console log its contents on init. 顺便说一句,由于我可以在init上控制台记录其内容,因此肯定是建立了控制器。 If i change the view name, the controller is not instantiated so I know the namespacing is working. 如果我更改视图名称,则不会实例化控制器,因此我知道命名空间正在工作。

thanks in advance! 提前致谢!

I'm not sure to embrace the whole problem, but for now, when you define your controller, in the init() function, first don't forget to call this._super() (it will go through the class hierarchy and call the constructors). 我不确定要解决整个问题,但是现在,当您在init()函数中定义控制器时,首先不要忘记调用this._super()(它将遍历类层次结构并调用构造函数)。 Maybe that's just the missing thing. 也许那只是丢失的东西。

Edit: it seems like with the new router, defining a view as a CollectionView does not work. 编辑:似乎与新的路由器,将视图定义为CollectionView不起作用。 so I replaced it with a normal Ember.View, and use an {each} helper in the template. 因此,我将其替换为普通的Ember.View,并在模板中使用了{each}帮助器。

<script type="text/x-handlebars" data-template-name="three">
  {{each controller itemViewClass="App.FooView" tagName="ul"}} 
</script>

here is a minimal working fiddle: http://jsfiddle.net/Sly7/qCdAY/14/ 这是一个最小的工作提琴: http : //jsfiddle.net/Sly7/qCdAY/14/

EDIT 2: 编辑2:

By re-reading the question, and seeing you try to bind the CollectionView content's property to controller, I tried it, because it just work fine :) 通过重新阅读问题,并看到您尝试将CollectionView内容的属性绑定到控制器,我尝试了它,因为它可以正常工作:)

http://jsfiddle.net/Sly7/qCdAY/15/ http://jsfiddle.net/Sly7/qCdAY/15/

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

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