是否可以使用与其父级不同的控制器创建子视图?
具体来说,我的应用程序有groups和users.查看单个组由App.GroupView连接到App.GroupController,它是Ember.ObjectController(因为组是对象)的子类.
在组视图中,我想要另一个视图,显示组内用户的列表.所以我的group.handlebars模板看起来像这样:
<header>
<h1>{{name}}</h1>
<p>{{description}}</p>
</header>
{{view App.GroupUsersView}}
Run Code Online (Sandbox Code Playgroud)
我想要做的是连接App.GroupUsersView到自己的控制器,它将是一个子类App.ArrayController,因为它代表了一个用户列表.并且其内容将设置users为该组的属性.
我怎么能做到这一点?
我试图循环我从另一个控制器获取的内容并修改一些属性forEach().但是这不起作用,并且在forEach循环中获取属性的任何尝试都会返回undefined.
所以这就是我试图做的事情:
this.get("controllers.index.arrangedContent").我的模特:
App.Data = DS.Model.extend({
name: DS.attr('string')
});
Run Code Online (Sandbox Code Playgroud)在我的控制器中:
App.StartController = Ember.ObjectController.extend({
needs: 'index',
alert: function() {
// get the "content" from the IndexController;
var names = this.get("controllers.index.arrangedContent");
var formattedNames = [];
names.forEach(function(item) {
var name = item.name; // <--returns "undefined"
name += "!";
formattedNames.push(name);
});
alert(formattedNames[0] );
}
});
Run Code Online (Sandbox Code Playgroud)
问题:有没有办法在forEach循环中访问属性"name" ,返回存储在模型上的正确值?
注意:首先我认为这将是本地存储适配器上的错误,所以我尝试使用后端的"fixture适配器",但这也不起作用.
但是,如果我在路由器上硬编码一些模型实例,如:
App.IndexRoute = Ember.Route.extend({
model: function() {
return [Ember.Object.create({id: 523, name: "John"}), …Run Code Online (Sandbox Code Playgroud) 在最新的ember-data 1.0发布之后,我在创建记录时遇到了一些问题.我在发行说明中读到了 - https://github.com/emberjs/data/blob/master/TRANSITION.md:
App.NewPostRoute = Ember.Route.extend({
model: function() {
return App.Post.createRecord();
}
});
Run Code Online (Sandbox Code Playgroud)
现在替换为:
App.NewPostRoute = Ember.Route.extend({
model: function() {
return this.store.createRecord('post');
}
});
Run Code Online (Sandbox Code Playgroud)
但是在我的控制器中我无法弄清楚如何调用createRecord()方法,因为我有这样的东西:
addNewTrip:function() {
var tripDeparature = this.get("tripDeparature");
var tripArrival = this.get("tripArrival");
var trips = App.Trips.createRecord({
tripDeparature: tripDeparature,
tripArrival:tripArrival,
isCompleted:false
});
trip.save();
this.set("tripDeparture","");
this.set("tripArrival","");
}
Run Code Online (Sandbox Code Playgroud)
并且它抛出一个错误:...没有方法'createRecord'(在新版本之后是预期的),但我无法弄清楚如何正确调用createRecord.任何帮助是极大的赞赏.
我有一个在用户计算机上运行的 Ember 应用程序(带有 ember 数据)。他们的机器上运行着一个 API 服务器,还有一个在线运行。我需要允许用户选择模型的持久保存位置(在其计算机上运行的 API 或在线 API)。因此,有两个 API 主机:
http://localhost:3000
and
http://api.example.com
Run Code Online (Sandbox Code Playgroud)
当用户创建记录时,他们可以设置是将记录保存在本地(通过本地 API 服务器)还是在线保存。我将此选择保留为记录上名为 dataSource 的值。
因此,根据记录数据源,我需要将模型的 ember RestAdapter 主机设置为正确的值。我知道可以根据模型覆盖适配器。例如,我可以创建一个 RecordAdapter 并手动将主机设置为一个值。但是,主机取决于记录中的值,并且我不确定如何使用 Ember Data 来完成此操作,因为其余适配器“主机”是一个属性,而不是一个函数。
http://emberjs.com/api/data/classes/DS.RESTAdapter.html#property_host
用户流程示例:
我创建了一个组件,它生成一个下拉列表,用于根据传入的类型从可能的项目列表中选择项目:
{{item-selector type="type1"}}
Run Code Online (Sandbox Code Playgroud)
我不想在页面加载时加载项目,因为它们可能有很多,很多时候它们不需要加载.我更愿意在用户与组件的交互中加载它们.那个nixes在路线中加载它们.
由于组件无法访问商店,我一直在尝试传入项目的控制器:
{{item-selector type="type1" widget=controllers.type1}}
Run Code Online (Sandbox Code Playgroud)
我正在使用widget因为我无法想到更好的属性名称和控制器已经采取.建议欢迎.
但现在我不确定如何在控制器中加载模型.我应该这样做init吗?或者也许是计算属性?我不确定.
关于如何解决这个问题的建议将不胜感激.
我试图破坏一个记录,我得到这个错误
An adapter cannot assign a new id to a record that already has an id.
[…] had id: 25 and you tried to update it with null. This likely happened because
your server returned data in response to a find or update that had a different
id than the one you sent.
Run Code Online (Sandbox Code Playgroud)
我的REST API返回一个200带有空对象响应的状态代码{}.我认为这就是问题所在,所以我一直在尝试自定义几个序列化程序挂钩(normalizeDeleteRecordResponse,extractDeleteRecord甚至只是normalizeResponse),但实际上并没有调用它们.
看看我的堆栈跟踪,错误似乎在didSaveRecord钩子中,我假设它正在接收空的JSON有效负载并将其传递给updateId.
我正在使用一个名为 ember-power-select 的插件。
Github: https: //github.com/cibernox/ember-power-select
用于搜索的文档:https ://ember-power-select.com/docs/the-search
多项选择的文档:https ://ember-power-select.com/docs/multiple-selection/
问题:如何使用nameor进行搜索email
/template.hbs
<PowerSelectMultiple
@searchEnabled={{true}}
@options={{this.authors}}
@selected={{this.author}}
@searchField="email"
@placeholder="Select some names..."
@onChange={{fn (mut this.author)}} as |author|>
{{name}}
</PowerSelectMultiple>
Run Code Online (Sandbox Code Playgroud)
/controller.js
authors = [
{ name: 'Foo', email: 'foo@gmail.com' },
{ name: 'Bar', email: 'bar@gmail.com'},
{ name: 'Baz' email: 'baz@gmai.com'}
]
Run Code Online (Sandbox Code Playgroud)