【问题标题】:sencha touch :: associated store/model pair not creating instance name functionsencha touch :: 关联商店/模型对未创建实例名称功能
【发布时间】:2011-09-19 09:56:10
【问题描述】:

我在设置关联的商店/模型概念时遇到了一个大问题。

我有模型/商店

app.models.User = Ext.regModel('app.models.User', {
    fields: [
        {name: 'id',         type: 'int'},
        {name: 'userID',    type: 'string'},
        {name: 'userName',  type: 'string'}
    ],

    associations: [
        {type: 'hasMany', model: 'app.models.Friend', name:'friends', associationKey:'friends'},
    ]
});

app.models.UserFriendsList = Ext.regModel('app.models.UserFriendsList', {
    fields: [
        {name: 'id',         type: 'int'},
        {name: 'userID',     type: 'string'},
        {name: 'userName',  type: 'string'}
    ],

    belongsTo: 'app.models.User',

    associations: [
        {type: 'hasMany', model: 'app.models.Friend',    name: 'friend'}
    ]
});

app.models.Friend = Ext.regModel('app.models.Friend', {
    fields: [
        {name: 'id',         type: 'int'},
        {name: 'userID',    type: 'string'},
        {name: 'userName',  type: 'string'}
    ],
   belongsTo: 'app.models.UserFriendsList',
});

app.stores.userStore = new Ext.data.Store({

    model: 'app.models.User',

    data:[
        {
            "id":                 "01", 
            "userName":         "Username"

            "friends": [
                {    "id":                 "02", 
                    "userName":         "Friend 1"
                },
                {    "id":                 "03", 
                    "userName":         "Friend 2"
                },
            ]

        }
    ]
}); 

然后我用

var currentUser = app.stores.User.getAt(0); 
console.log(currentUser);
var friends = currentUser.friends().getAt(0);
console.log(friends); 

跟踪有关当前加载用户的一些调试信息。

当我使用不带“name”参数的“associationKey”参数时,跟踪显示数据已正确提取到跟踪对象“currentUser”的数据部分中。但是没有函数 friends() 可以使用。我只得到错误

TypeError: Result of expression 'currentUser.friends' [undefined] is not a function.

当我使用没有“associationKey”参数(或两个参数)的“name”参数时,会创建实例函数 friends(),但不会将数据提取到对象中,因此我无法使用它。

我的代码有什么问题?或者我对概念的误解在哪里?

THNX!!!

编辑:请查看我的文件:http://www.box.net/shared/n4h3rdp1499ihx990v3h

【问题讨论】:

    标签: model associations sencha-touch store


    【解决方案1】:

    您的模型声明和has many associations 似乎有一些问题。

    首先,您的模型名称应该只是简单的陈述,而不是完全限定的对象。它使声明更容易。

    app.models.User = Ext.regModel('app.models.User', {
    

    应该是:

    app.models.User = Ext.regModel('User', {
    

    其次,您需要正确命名将 id 相互关联的字段。

    app.models.Friend = Ext.regModel('Friend', {
    fields: [
        {name: 'user_id', type: 'int'},
        .....
    ],
    

    现在您的模型命名为User,并且您的朋友模型引用user_id,Sencha Touch 将自动正确使用关联。

    第三,您需要修正您的协会声明。您将属性 associationKey 添加到关联中。在我提到的第一点之后,文档中不存在该模型,并且模型应该只是“朋友”。

    associations: [
        {type: 'hasMany', 
         model: 'Friend', 
         name:'friends', 
        },
    ]
    

    第四,要获得您需要在商店中加载的朋友。 currentUser.friends().load().getAt(0);

    您可以花哨的方式将不同的外键声明为不同的名称,但在这一点上,我将启动基本示例并开始工作。

    Sencha 有一个很好的关于associations and validations 入门的教程。

    【讨论】:

    • +1 on load()-comment,整天都在找这个。我认为它在文档中没有正确定义。
    • @martijn 是的,您必须这样做绝对不清楚。使用关联属性会导致返回的 Ext.Store 将 autoload 设置为 false,因此您必须自己进行加载。
    • 嘿,谢谢!我现在就试试。我需要每次加载商店还是只加载一次?
    • hm 我仍然得到 boldTypeError: Result of expression 'currentFriend.friends' [undefined] is not a function.**bold** 当我执行 code var currentFriend = app.stores.userStore.findRecord('id', id); var theFriends = currentFriend.friends().load(); code 仍然没有生成关联的存储,也没有填充存储对象。
    • 我不知道是什么导致了这些问题。尝试使用桌面浏览器进行调试。关联对象在对象上为currentUser.associations()friends() 函数也应该在对象的原型上,看看它是否存在。我会从那里开始,如果情况变得更糟,请通过 sencha 代码跟踪找出您的对象未正确创建的原因
    猜你喜欢
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    相关资源
    最近更新 更多