- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
错误如下: http://puu.sh/lXzja/f773fb6c9a.png
我的用户模型的主键是用户名。我的路线的主键是路线名称。我的 api 根据 jsonapi.org 规范返回 data:{} 内的 json。因此 id 属性并不像 js-data 所要求的那样位于顶层。这就是为什么我在“users”的 afterFind 中返回 data.data 的原因。我尝试在“路线”中做类似的事情,但它是一系列路线。beforeInject 中的控制台登录给了我:
这是配置:
DS.defineResource({
name: 'users',
idAttribute: 'username',
basePath: apiEndpoint,
relations: {
hasMany: {
routes: {
localField: 'routes',
foreignKey: 'username'
}
}
},
// set just for this resource
afterFind: function(resource, data, cb) {
// do something more specific to "users"
cb(null, data.data);
}
});
DS.defineResource({
name: 'routes',
idAttribute: 'routename',
basePath: apiEndpoint,
cacheResponse: true,
relations: {
belongsTo: {
users: {
parent: true,
localKey: 'username',
localField: 'users'
}
}
},
beforeInject: function(resource, data) {
// do something more specific to "users"
console.log(data);
return data.data.routes;
}
});
这是我尝试加载路线的地方,但出现错误:
resolve: {
user: function($route, DS) {
var username = $route.current.params.username;
return DS.find('users', username).then(function(user) {
DS.loadRelations('users', user.username, ['routes']).then(function(user) {
console.log(user);
}, function(err) {
console.log(err);
});
});
}
}
最佳答案
您的数据不仅嵌套在“数据”字段下,而且嵌套在“路线”字段下。因此,当您找到路线时,您将尝试注入(inject)类似以下内容的内容:
{
routes: [{
// foreignKey to a user
username: 'john1337',
// primary key of a route
id: 1234
}]
}
当你需要注入(inject)时:
[{
username: 'john1337',
id: 1
}]
将路线资源上的 afterFindAll
添加到 cb(null, data.data.routes)
。
您需要:
A) 在您的所有资源中添加大量“after” Hook ,或者B) 使反序列化变得通用,以便它适用于所有资源。也许是这样的?
DS.defaults.afterFind = function (Resource, data, cb) {
cb(null, data.data[Resource.name])
};
DS.defaults.afterFindAll = function (Resource, data, cb) {
cb(null, data.data[Resource.name])
};
关于angularjs - JS数据错误: attrs must contain the property specified by idAttribute - with hasMany relations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34311333/
在集合的上下文中,我想根据包含模型数据的某些对象检索模型实例,但我不想对 idAttribute 进行硬编码。 当你已经有一个模型实例时,Backbone 使事情变得简单,你可以访问它的 .id 属性
我认为我的问题出在解析函数中: /*global define */ define([ 'jquery', 'underscore', 'backbone', 'mod
我正在使用 mongoLab,模型 ID 如下所示 "_id": { "$oid": "50f9a0f5e4b007f27f766cf3" }, 我正在使用 idAttribute 将模型 ID
我的模型结构如下: model = { distance: 12.05, widget: { id: 1, creationDate: '12/01/2
我的 Backbone 模型的idAttribute可以是任何东西(Backbone的默认值是“id”)。我想在将模型插入 Backbone 集合之前检查该集合是否已包含具有相同 idAttribut
我正在使用主干集合从服务器获取一个 mongodb 集合。由于 ID 存储为“_id”,我使用 idAttribute 将其映射到“_id”。 (function(){ var PlaceMode
我正在使用带有主干的 MongoDb/Lithium php 框架。 我希望我所有的模型 ID 和 idAttribute(s) 与我的数据库文档 ID 相同(即“_id”/mongoid) 问题:
我从 API 创建新模型,API 有一个 idAttribute ID。这是我的模型: class Video extends Backbone.Model idAttribute: 'ID'
我有一个集合,它指定一个模型,该模型本身有一个 idAttribute 集,因为我的后端使用主键的自定义字段。代码是这样的: var SearchItemModel = Backbone.Model.
我在访问模型中的 ID 时遇到问题。我知道模型可以有一个我定义的属性 idAttribute 。我的问题是我想使用模型的 id 进行查询,但我不知道如何操作。我已阅读 bookshelfJS 文档,但
我刚刚将 idAttribute: '_id' 添加到 BackboneJS 应用程序中的某些模型,以便在服务器端与 MongoDB 一起使用。起初,这按预期工作,稍后在 View 中查看模型时,th
我已将 idAttribute 行添加到此代码中,希望它允许我为我的 id 字段指定一个备用名称: bookshelf.ApiUser = Bookshelf.Model.extend({ t
我已经研究 Backbone.js 几个星期了,我对将 View 与模型、路由器和集合一起使用感到很自在。 我还有一些很大的差距: id、cid、idAttribute之间有什么联系?它们如何相互影响
我正在尝试更新主干模型,服务器端是 asp.net mvc 4。我得到: "System.ArgumentException: An item with the same key has alread
我在尝试将多个模型从服务器添加到集合时遇到错误。它抛出错误: Uncaught TypeError: Cannot read property 'idAttribute' of undefined 这
我是 appcelerator 的新手。在遵循快速入门指南的同时,我无法在 iOS 模拟器上生成构建,即使在将代码完全复制粘贴到所需模型和 Controller 之后也是如此。 控制台给我以下信息:
错误如下: http://puu.sh/lXzja/f773fb6c9a.png 我的用户模型的主键是用户名。我的路线的主键是路线名称。我的 api 根据 jsonapi.org 规范返回 data:
我正在努力学习 Backbone.js 它可以获取、放置和删除单个模型。但是,当我创建一个集合时,fetch 方法给出了这个错误:未捕获的类型错误:无法读取未定义的属性“idAttribute”(ba
我有两个模型(用户和任务),它们是 Backbone.RelationalModel 的实例. 这两个模型的关系如下: // Task model var Task = Backbone.Re
我是一名优秀的程序员,十分优秀!