App.Router.map(function() {
this.resource('documents', { path: '/documents' }, function() {
this.route('edit', { path: ':document_id/edit' });
});
this.resource('documentsFiltered', { path: '/documents/:type_id' }, function() {
this.route('edit', { path: ':document_id/edit' });
this.route('new');
});
});
Run Code Online (Sandbox Code Playgroud)
这个控制器带有一个子视图事件,基本上转换为过滤后的文档
App.DocumentsController = Ember.ArrayController.extend({
subview: function(context) {
Ember.run.next(this, function() {
//window.location.hash = '#/documents/'+context.id;
return this.transitionTo('documentsFiltered', context);
});
},
});
Run Code Online (Sandbox Code Playgroud)
我的问题是,当页面哈希值更改时,此代码可以正常工作。
但是当我运行上面的代码而不使用 location.hash 位并且使用 Ember 本机时,transitionTo我得到了一个神秘的信息
未捕获的类型错误:对象 [object Object] 没有方法“切片”
有什么线索吗?
谢谢
更新:
App.DocumentsFilteredRoute = Ember.Route.extend({
model: function(params) {
return App.Document.find({type_id: params.type_id});
},
});
{{#collection contentBinding="documents" tagName="ul" class="content-nav"}}
<li …Run Code Online (Sandbox Code Playgroud) 我有一个包含许多消息的游戏模型。为了让它们实时同步,我使用了这样的计算属性:
gameMessages: function () {
var gameId, messages;
gameId = this.get('id');
return this.get('store').filter('message', function (message) {
return (message.get('game.id') === gameId);
});
}.property('messages'),
Run Code Online (Sandbox Code Playgroud)
如何限制它返回的消息数量?我只想展示 10 个最新的。
我有一个有点独特的情况,我为 ember-data 编写了一个缓存层,将记录序列化到本地存储。当我将缓存反序列化为我使用的 ember-data 模型时this.store.createRecord('model_name', cacheData);。我正在缓存的这些记录在服务器上具有现有值。这对我来说很有效,直到我想要save()记录为止。保存时认为该记录是一条新记录,即使它具有“id”属性。当save()调用时,会向我的应用程序服务器发出 POST 而不是 PUT。有谁知道如何将商店中的记录标记为非新记录。
我的drf后端有这个模型:
class Product:
price_range = ...
Run Code Online (Sandbox Code Playgroud)
我在JSONApi序列化程序中使用EmberData。我刚刚发现JSON API需要使用反斜线属性。所以我需要告诉Drf:
JSON_API_FORMAT_KEYS = 'dasherize'
Run Code Online (Sandbox Code Playgroud)
并且该属性被序列化为JSON:
price-range
Run Code Online (Sandbox Code Playgroud)
然后,EmberData进行舞蹈,然后得到Ember模型属性:
import DS from 'ember-data';
export default DS.Model.extend({
...
priceRange: DS.attr('number'),
...
});
Run Code Online (Sandbox Code Playgroud)
(如果我没记错的话,那是旧的RESTSerializer期望)priceRangeJSON
因此,我们从price_range-> price-range-> priceRange(如果您问我,这太疯狂了)。我通过反复试验发现了所有这些。对于drf,此处记录了相应的设置。
这哪里是记录了JSONApi,EmberData和Ember?我想确保我已经真正理解了这一点,并且关系也是如此。相关的drf配置设置为:
JSON_API_FORMAT_RELATION_KEYS = 'dasherize'
Run Code Online (Sandbox Code Playgroud) 在铁轨中,
js文件位于
app/assets/javascripts
Run Code Online (Sandbox Code Playgroud)
同样在ember-cli应用程序中添加js和css文件的位置?
我有两个模型员工和 empdetails
//employee model
import DS from 'ember-data';
export default DS.Model.extend({
empId : DS.attr(),
password : DS.attr(),
email : DS.attr(),
empdetails : DS.belongsTo("empdetails")
});
//empdetails model
import DS from 'ember-data';
export default DS.Model.extend({
firstName : DS.attr(),
lastName : DS.attr(),
dateOfJoining: DS.attr(),
employee : DS.belongsTo("employee")
});
Run Code Online (Sandbox Code Playgroud)
我使用 RESTAdapter 进行 REST 调用。
//serializer
import DS from 'ember-data';
export default DS.JSONSerializer.extend({
});
Run Code Online (Sandbox Code Playgroud)
当我尝试为员工发出获取请求时,出现以下错误
请检查您的序列化程序并确保它将关系负载序列化为 JSON API 格式。错误:断言失败:遇到了一个没有属于属于关系的类型的关系标识符 'empdetails' on ,需要一个类型为 'empdetails' 的 json-api 标识符,但找到了 '{"id":"1","firstName":"xxx "}
我从我的后端得到以下 JSON
[
{
"id": 1,
"email": …Run Code Online (Sandbox Code Playgroud) 目前,当一篇文章添加到商店时,当我在路由中使用 store.query() 过滤服务器端时,我的视图不会更新,但当我使用 store.findAll() 过滤客户端时,它会更新。
使用findAll,过滤客户端
//route.js
model() {
return this.get('store').findAll('article');
}
//controller.js
articleSorted: computed.filterBy('model', 'isPublished', true),
Run Code Online (Sandbox Code Playgroud)
并带有查询过滤服务器端
//route.js
model() {
return this.get('store').query('article', { q: 'isPublished' }),
}
Run Code Online (Sandbox Code Playgroud)
事实是 findAll 正在重新加载,而 query 则没有。
我有一个 Ember 模型,有一个我想显示和使用的字段,但在我保存或从服务器检索它之前,我想将它乘以/除以 1000。
这意味着 UI 应该始终使用较小的值,但将较大的值保存到服务器。同样,它应该从服务器检索一个更大的值,但在允许它被控制器、路由等使用之前使它更小。
// models/my_model.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
value: DS.attr('number')
});
Run Code Online (Sandbox Code Playgroud)
本质上,我希望我的应用程序将这些字段显示为:
/* my webpage */
Please enter name: "foo"
Please enter value: 5
Run Code Online (Sandbox Code Playgroud)
但是当我发送请求时,它应该将它们发送为:
{
"my_model": {
"name": "foo",
"value": 5000
}
}
Run Code Online (Sandbox Code Playgroud)
它应该也收到他们以这种格式,但随后由1000除以deserialise值。
请注意,乘以/除以 1000 是一个示例 - 我可能想要添加、添加、添加、减去等。
我尝试以不太优雅的方式执行此操作:
// Controller.js
actions: {
save: function() {
this.set('model.value', this.get('model.value') * 1000);
this.get('model').save().then(
/* stuff */
);
this.set('model.value', this.get('model.value') / 1000);
}
}
Run Code Online (Sandbox Code Playgroud)
但我对此并不满意,它会导致难以维护且容易出错的重复代码。
我有一个RESTSerializer,但我不知道如何使用它来操作字段,并且在 …
采用最新(从GitHub今天)余烬和灰烬的数据,这段代码是给我一个错误 - 遗漏的类型错误:无法在余烬,数据读取latest.js的未定义的属性"发现":3170
// only needed on JSFiddle; this is to trigger the route
history.pushState("", "", "/projects/1/tasks");
App = Ember.Application.create({});
App.store = DS.Store.extend({
revision: 4,
adapter: DS.RESTAdapter.create()
});
App.Project = DS.Model.extend({
name: DS.attr('string')
});
App.ApplicationController = Ember.ObjectController.extend({
});
App.ApplicationView = Ember.View.extend({
templateName: 'application'
});
App.TasksView = Ember.View.extend({
templateName: 'tasks'
});
App.Router = Ember.Router.extend({
root: Ember.Route.extend({
tasks: Ember.Route.extend({
route: '/projects/:project_id/tasks',
connectOutlets: function(router) {
router.get('applicationController').connectOutlet('Tasks');
}
})
})
});
App.router = App.Router.create({
location: 'history'
});
App.initialize(App.router);
Run Code Online (Sandbox Code Playgroud)
改变:在路径中的projects_id到其他东西(例如1,所以它匹配URL)修复了这个,但当然这不是很有用.
任何人都可以了解这里发生的事情吗?谢谢!
我的代码:http: //jsbin.com/axaqix/20/edit
我的路由器:
WebApp.Router.map(function () {
this.resource('sites', { path: '/' } , function() {
this.resource('site', { path: '/sites/:site_id'} , function() {
this.resource('posts', { path: 'posts' });
});
this.route('new', {path: 'new'});
});
});
Run Code Online (Sandbox Code Playgroud)
我的模板:
<!--Main Page-->
<script type="text/x-handlebars" data-template-name="sites">
<ul>
{{#each site in controller}}
<li>
{{#linkTo 'site' site}} {{site.siteName}} {{/linkTo}}<br>
{{#linkTo 'posts' site}} go to posts {{/linkTo}}
</li>
{{/each}}
</ul>
{{#linkTo 'sites.new'}} ADD NEW WEBSITE {{/linkTo}}
{{outlet}}
Run Code Online (Sandbox Code Playgroud)
<!--Displays site details-->
<script type="text/x-handlebars" data-template-name="site">
<p>Site Details</p>
Name: {{siteName}} <br>
Secret …Run Code Online (Sandbox Code Playgroud)