【问题标题】:Ember Error in Route Model路径模型中的 Ember 错误
【发布时间】:2015-11-29 22:39:11
【问题描述】:

我正在深入研究 Ember,并尝试为我的路线模型提供内容列表。这是我的 App.js 代码:

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

    App.Router.map(function() {
        this.route('about');
        this.resource('blogs');
        this.resource('projects');
    });

    App.PROJECTS = [
        {
            "id": 1,
            "title": "SnapFoo - SVG Animation Library",
            "textone": "SnapFoo",
            "texttwo": "SVG Animation Library",
            "image": "snapfoo.jpg"
        }
    ];

    App.ProjectsRoute = Ember.Route.extend({
      model: function() {
        return App.PROJECTS;
      }
    });

PROJECTS 数组在内容方面是缩写的,但这就是格式。现在,我正在尝试在 Hanldebars 模板中运行我的每个循环,如下所示:

<script type="text/x-handlebars" data-template-name="projects">
 {{#each}}
  <div class="project-icon">
   <span>{{textone}}</span>
   <span>{{texttwo}}</span>
   <a href="project.php?id=1"><img {{bind-attr alt="title" src="image"}}/></a>
  </div>
 {{/each}}
</script>

但是,这样做时,我收到的错误是 Uncaught TypeError: Cannot read property 'addDependency' of undefined 来自 ember.min.js。

如果我删除 {{each}} 循环,错误就会消失,这让我相信这与路线中的模型有关。但任何建议都会非常有帮助。谢谢!

【问题讨论】:

    标签: javascript ember.js routes handlebars.js


    【解决方案1】:

    您在视图中缺少模型,您正在迭代 {{each}}

    在这种情况下,您需要将变量放入您的model

    <script type="text/x-handlebars" data-template-name="projects">
     {{#each model as |project|}}
      <div class="project-icon">
       <span>{{project.textone}}</span>
       <span>{{project.texttwo}}</span>
       <a href="project.php?id=1"><img {{bind-attr alt="title" src="image"}}/></a>
      </div>
     {{/each}}
    </script>
    

    我创建了一个example,您可以在其中看到此代码的工作原理,它与您想要实现的非常相似。

    【讨论】:

    • 这确实让我更接近了。但是,这并不接近我在 CodeSchool 上学习的语法。这是 Ember 文档中的操作方式吗?最后,我在图像中的 bind-attr 不会绑定任何东西,即使是硬编码的字符串。
    • 我正在遵循 ember-cli 语法,它带来了所有对 ES6 的喜爱。关于bind-attr 这里的修复&lt;a href="project.php?id=1"&gt;&lt;img {{bind-attr alt="title" src="project.image"}}/&gt;&lt;/a&gt; 请注意,我已经通过project.image 更改了图像,这是循环内的范围变量。这里example 更新了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多