Ember.js:如何将我的组件与模型相关联? [英] Ember.js: How do I associate my component with a model?

查看:92
本文介绍了Ember.js:如何将我的组件与模型相关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ember.js的新手,我试图弄清楚在这一点上如何将东西拼凑在一起。我构建的一个组件,因为我需要一个可重用的小部件,用于我应用程序的许多部分,是一个站点导航小部件。基本上,当您打开一个新问题:问题标签用户徽章未回答时,它几乎像您在StackOverflow中看到的按钮/链接。在我的应用程序中,这些链接在服务器端具有与它们相关联的名称和id。我想要能够在我的应用程序的多个部分使用这个导航小部件,它应该是简单的:

I am new to Ember.js and am trying to figure out how to piece things together at this point. One component I built, since I need a re-usable "widget" to use across many portions of my application, is a "site nav" widget. Basically, it's almost like the buttons/links you see in StackOverflow when you open a new question titled: "Questions Tags Users Badges Unanswered". In my app, these links have a name and id associated with them on the server-side. I want to be able to use this navigation widget on multiple parts of my app and it should be as simple as putting:

{{site-导航}}

到模板中。我得到的那部分工作很好,但导航目前是硬编码的把手。我的问题是,对于组件,从服务器检索/填充模型数据的正确位置在哪里?对于控制器,我们直接从控制器的路由定义。组件不与路由器相关联。事实上,如前所述,它可以在应用程序的几个部分重新使用。

into a template. I got that part working just fine, but the navigation is currently hard coded in handlebars. My question is, for a component, where is the right place to retrieve/populate model data from the server? For a controller, we do it directly from the controller's route definition. The component is not associated with a router. In fact, it can be re-used, as mentioned before, in several parts of the app.

我想将这个组件放在模板中,并从服务器中填入模型导航,该服务器具有我需要的导航的名称/ ID。这是最好的地方在哪里?我猜想我仍然会从$ code> DS.Model 之类的东西中扩展,但是我不完全确定在哪里/何时/如何将它与组件集成在一起。什么时候创建模型并调用 .find()类型调用服务器来填充 site-nav 数据?

I want to be able to drop this component into templates and have it populated with modeled nav from the server which has the name/IDs of the navigation I need. Where is the best place to do this? I'm guessing I'll still extend from something like DS.Model, but I'm not entirely sure where/when/how to integrate this with the component. When do I create the model and invoke a .find() type call to the server to populate site-nav with data?

推荐答案

您可以将值传递给组件将属性传递给组件
通过句柄。

you can pass value to component Passing properties to component via handlebars.

{{my-nav navlist=listfromserver}}

所以从服务器列表中我们的控制器可以传递给组件

so the list from server is in our controller can be passed to the component

这篇关于Ember.js:如何将我的组件与模型相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆