Ember错误说我没有时指定了hasMany

时间:2014-01-02 18:23:59

标签: ember.js ember-data

在这个尝试过的Ember 1.2应用程序中完全不知所措。我根据Ember喜欢的方式格式化了JSON,并且我已经按照指南中的所有说明来设置我的模型,但是我得到了一些奇怪的错误。

这是我的JSON:

{ "candidates" : [ { "id" : 796,
    "name" : "Townsend, Matt",
    "party" : 7
  },
  { "id" : 797,
    "name" : "Griffiths, Robert",
    "party" : 54
  },
  { "id" : 795,
    "name" : "Burke, George",
    "party" : 11
  },
  { "id" : 794,
    "name" : "Zeigler, Simon",
    "party" : 4
  },
  { "id" : 793,
    "name" : "Aslam, Farida",
    "party" : 10
  },
  { "id" : 792,
    "name" : "Hannigan, Dominic",
    "party" : 3
  },
  { "id" : 791,
    "name" : "Hoare, Simon",
    "party" : 1
  },
  { "id" : 798,
    "name" : "Brennan, Kevin",
    "party" : 2
  },
  { "id" : 803,
    "name" : "Griffiths, Jake",
    "party" : 7
  },
  { "id" : 802,
    "name" : "Henessey, Mike",
    "party" : 4
  },
  { "id" : 801,
    "name" : "Islam, Mohammed Sarul",
    "party" : 10
  },
  { "id" : 800,
    "name" : "Hitchinson, Rachael",
    "party" : 3
  },
  { "id" : 799,
    "name" : "Jones-Evans, Angela",
    "party" : 1
  }
],
"constituencies" : [ { "candidates" : [ 797,
        796,
        795,
        794,
        793,
        792,
        791
      ],
    "id" : 128,
    "name" : "Cardiff South and Penarth"
  },
  { "candidates" : [ 803,
        802,
        801,
        800,
        799,
        798
      ],
    "id" : 129,
    "name" : "Cardiff West"
  }
],
"parties" : [ { "abbreviation" : "Green",
    "id" : 7
  },
  { "abbreviation" : "Comm",
    "id" : 54
  },
  { "abbreviation" : "Ind",
    "id" : 11
  },
  { "abbreviation" : "UKIP",
    "id" : 4
  },
  { "abbreviation" : "PC",
    "id" : 10
  },
  { "abbreviation" : "LD",
    "id" : 3
  },
  { "abbreviation" : "C",
    "id" : 1
  },
  { "abbreviation" : "Lab",
    "id" : 2
  }
],
"responses" : [ { "candidate" : 796,
    "constituency" : 128,
    "id" : 1,
    "will_vote" : true
  },
  { "candidate" : 798,
    "constituency" : 129,
    "id" : 2,
    "will_vote" : true
  }
]
}

这是我的模特:

Poll.Response = DS.Model.extend(
  willVote: DS.attr 'boolean'
  candidate: DS.belongsTo 'candidate'
  constituency: DS.belongsTo 'constituency'
)

尝试加载数据时,我会收到以下错误:

  
      
  • 断言失败:您在Poll.Response上指定了一个hasMany(候选人)但未找到候选人
  •   
  • 断言失败:尝试在Poll.Response上加载候选者,但该类型不存在。
  •   
  • 断言失败:您在Poll.Response上指定了一个hasMany(选区)但未找到选区。
  •   

我已经搜索了这些错误的解释,我找不到任何东西,而且我特别感到困惑的是,当我明确没有时,我指定了一个hasMany。

如果我从JSON和belongsTo行中删除关联,它似乎工作正常。

我做错了什么?我非常感谢任何帮助!


根据kingpin2k的要求,这是我的商店档案:

DS.RESTAdapter.reopen
  namespace: 'api/v1'

Poll.Store = DS.Store.extend(
  adapter: DS.RESTAdapter.create()
)

以下是其他型号:

Poll.Constituency = DS.Model.extend(
  name: DS.attr 'string'
  candidates: DS.hasMany 'candidate'
  responses: DS.hasMany 'response'
)

Poll.Candidate = DS.Model.extend(
  name: DS.attr 'string'
  responses: DS.hasMany 'response'
  constituency: DS.belongsTo 'constituency'
  party: DS.belongsTo 'party'
)

2 个答案:

答案 0 :(得分:0)

好的,所以这是一个两个部分,一个你发现它告诉你的错误中的错误,它说它找不到hasMany,但该断言适用于hasManybelongsTo关系。

意味着找不到您的其他模型(Poll.CandidatePoll.Constituency)。我会验证它们是否包含在页面中。

http://emberjs.jsbin.com/OxIDiVU/98/edit

答案 1 :(得分:0)

好的,原来并没有实际安装过ember-data。如果你看到像我这样的错误,请尝试运行

  

rails g ember:install --head

以获取所有内容的最新版本。

感谢kingpin2k帮助排除其他可能性!