gpt4 book ai didi

ember.js - 嵌入式 hasMany 属性访问给出了未定义的 "TypeError: Cannot call method ' hasOwnProperty'

转载 作者:行者123 更新时间:2023-12-04 15:24:32 28 4
gpt4 key购买 nike

使用:

  • Ember commit a5d45f66e1 from Jan 3, 2013)
  • Ember-Data commit 508479dee7 from Jan 4, 2013

  • 类似于 this question ('Unable to get hasMany association') ,我无法直接访问嵌入的 hasMany 记录,但可以通过模型的内容属性查看它们。

    对于 JSON:
    {
    "ref_book_search":{
    "query":"har",
    "results":[
    {
    "publisher":{
    "name":"Pangolin",
    "created":"2012-09-10T18:38:27.259515",
    "id":"3d2028e4fb91181e1a6e012313914f821",
    "is_active":true,
    "main_url":null,
    "resource_uri":"/api/v1/ref_publisher/3d2028e4fb91181e1a6e012313914f821"
    },
    "genre":"romcom",
    "id":"cc671f00fc2711e1e41612313914f821",
    "resource_uri":"/api/v1/ref_book/cc671f00fc2711e1e41612313914f821",
    "title":"Harry Placeholder and the Goblet of PBR"
    },
    {
    "publisher":{
    "name":"Hoof & Mouth",
    "created":"2012-10-10T14:31:27.259515",
    "id":"3d200e9afb9811e1a27417383914f821",
    "is_active":true,
    "main_url":null,
    "resource_uri":"/api/v1/ref_publisher/3d200e9afb9811e1a27417383914f821"
    },
    "genre":"horror",
    "id":"cc621f08fc2711e1b81612313914e821",
    "resource_uri":"/api/v1/ref_book/cc621f08fc2711e1b81612313914e821",
    "title":"Harvey Weinstein Holiday Cookbook"
    }
    ]
    }
    }

    和 app.js(注意 map 语句,这是上一个问题中建议的解决方案):
    var App = Ember.Application.create();

    DS.RESTAdapter.configure("plurals", {"ref_book_search" : "ref_book_search"});

    App.store = DS.Store.create({
    revision: 11,
    adapter: DS.RESTAdapter.create({
    bulkCommits: false,
    namespace: "api/v1"
    })
    });

    DS.RESTAdapter.map('App.RefBookSearch', {
    primaryKey: 'query'
    });

    App.store.adapter.serializer.map('App.RefBookSearch', {
    results: {embeddded: 'load'}
    });

    App.store.adapter.serializer.map('App.RefBook', {
    publisher: {embeddded: 'load'}
    });

    App.RefPublisher = DS.Model.extend({
    name : DS.attr('string'),
    created : DS.attr('date'),
    isActive : DS.attr('boolean'),
    mainUrl : DS.attr('string')
    });

    App.RefBook = DS.Model.extend({
    publisher: DS.belongsTo('App.RefPublisher'),
    title : DS.attr('string'),
    genre : DS.attr('string')
    });

    App.RefBookSearch = DS.Model.extend({
    query: DS.attr('string'),
    results: DS.hasMany('App.RefBook')
    });

    App.Router.map(function(match) {
    match('/').to('query'),
    match('/query/:ref_book_search_id').to('query')
    });

    App.QueryController = Ember.Controller.extend({
    bookSearch: null,
    results: []
    });

    App.QueryRoute = Ember.Route.extend({
    setupControllers: function(controller, refBookSearch) {
    controller.set('bookSearch', refBookSearch)
    controller.set('results', refBookSearch.get('results').content)
    }
    })

    App.initialize();

    一开始一切都很好,就像其他海报一样:
    search = App.RefBookSearch.find('ha')
    search.get('query')
    // => "ha"
    results = search.get('results')
    results.get('firstObject') instanceof App.RefBook
    // => true

    但是之后:
    results.forEach(function(result) { console.log(result.get('title')) })
    // => TypeError: Cannot call method 'hasOwnProperty' of undefined

    通过内容访问显示数据在那里:
    results.content.forEach(function(result) { console.log(result.title) })
    // => Harry Placeholder and the Goblet of PBR
    // => Harvey Weinstein Holiday Cookbook

    现在,如果我再次尝试直接访问,则会得到一个略有不同的错误:
    results.forEach(function(result) { console.log(result.get('title')) })
    // => undefined x 2

    这可能与 this bug有关,也可能无关。几天前提交的。

    我觉得我已经在这里尝试了一切;我希望我只是错过了一些简单的东西。任何指针都非常感谢。谢谢。

    最佳答案

    这就是最终对我有用的方法。似乎有一些操作顺序敏感性,即在创建商店之前进行配置和映射。另请注意,adapter.map 是一个在序列化程序上执行映射的便利函数。

    App.Adapter = DS.RESTAdapter.extend()

    App.Adapter.configure("plurals", {
    "ref_book_search" : "ref_book_search",
    "ref_book" : "ref_book",
    "ref_publisher" : "ref_publisher"
    });

    App.Adapter.configure('App.RefBookSearch', {
    primaryKey: 'query'
    });

    App.Adapter.map('App.RefBookSearch', {
    results: {'embedded': 'load'}
    });

    App.Adapter.map('App.RefBook', {
    publisher: {'embedded': 'load'}
    });

    App.store = DS.Store.create({
    revision: 11,
    adapter: App.Adapter.create({
    bulkCommits: false,
    namespace: "api/v1"
    })
    });

    关于ember.js - 嵌入式 hasMany 属性访问给出了未定义的 "TypeError: Cannot call method ' hasOwnProperty',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14174921/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com