骨干模型中没有属性

时间:2013-12-11 17:21:48

标签: ruby-on-rails json backbone.js marionette rabl

我正在查看来自http://www.backbonerails.com/的示例,它看起来完全相同。 让我从我的应用程序中为您绘制一张图片:

路线:

routes.rb:

Mario::Application.routes.draw do
  root to: 'application#index'

  resources :conferences
  resources :participations, only: [:create]
end

控制器:

class ConferencesController < ApplicationController
  respond_to :json

  def show
    @conference = Conference.find params[:id]
  end

  def index
    @conferences = Conference.all
  end
end

rabl模板:

index.json.rabl格式化的rsponse:

collection @conferences
attributes :id, :title
node(:date_from){|c| I18n.l c.date_from, format: :short}
node(:date_to){|c| I18n.l c.date_to, format: :short}
node(:lectures) do |c|
  c.lectures_by_day.map do |l|
    partial 'lectures/base', object: l
  end
end

响应

这是我从http://localhost:3000/conferences.json获得的回复:

[
  {
    "id": 1,
    "title": "Ruby on Rails for Dummies",
    "date_from": "11 Dec 12:00",
    "date_to": "13 Dec 17:00",
    "lectures": []
  }
]

骨干模型

这是应该从这里获取的Backbone模型:

@Mario.module "Entities", (Entities, App, Backbone, Marionette, $, _) ->

  class Entities.Conference extends Backbone.Model
    defaults:
      title: ''

  class Entities.ConferenceCollection extends Backbone.Collection
    model: Entities.Conference
    url: '/conferences.json'

...最后是JS控制台:

这就是我在js控制台中最终得到的结果:

cc=new Mario.Entities.ConferenceCollection()

▹ConferenceCollection {length: 0, models: Array[0], _byId: Object, constructor: function, model: function…}

cc.fetch()

此时,rails服务器响应(来自log/development.log

2013-12-11T18:07:55+01:00 [ INFO] 40497 : Started GET "/conferences.json" for 127.0.0.1 at 2013-12-11 18:07:55 +0100
2013-12-11T18:07:55+01:00 [ INFO] 40497 : Processing by ConferencesController#index as JSON
2013-12-11T18:07:55+01:00 [DEBUG] 40497 :   Conference Load (0.2ms)  SELECT "conferences".* FROM "conferences"
2013-12-11T18:07:55+01:00 [DEBUG] 40497 :    (0.1ms)  SELECT COUNT(*) FROM "lectures" WHERE "lectures"."conference_id" = 1 AND ("lectures"."date_from" BETWEEN '2013-12-11' AND '2013-12-12')
2013-12-11T18:07:55+01:00 [ INFO] 40497 :   Rendered conferences/index.json.rabl (14.2ms)
2013-12-11T18:07:55+01:00 [ INFO] 40497 : Completed 200 OK in 20ms (Views: 18.3ms | ActiveRecord: 0.5ms)

回到js控制台:

cc.models[0]

▹Conference {cid: "c6", attributes: Object, collection: ConferenceCollection, _changing: false, _previousAttributes: Object…}

cc.models[0].attributes

▽attributes: Object
  ▹_byId: Object
  ▹_onModelEvent: function (event, model, collection, options) {
  ▹_prepareModel: function (attrs, options) {
  ▹_removeReference: function (model) {
  ▹_reset: function () {
  ▹add: function (models, options) {
  ▹all: function () {
  ▹any: function () {
  ▹at: function (index) {
  ▹bind: function (name, callback, context) {
  ▹chain: function () {
  ▹clone: function () {
  ▹collect: function () {
  ...

等。等等 这些显然不是定义的模型属性。

到底是怎么回事?这应该是如此简单。我一定错过了什么,但是什么?为什么Backbone没有将正确的JSON结构解释为模型的属性?

2 个答案:

答案 0 :(得分:0)

看看here - 这对你来说应该很有趣:

config.include_json_root = false

我想你必须在你的配置中改变它,你应该好好去。

答案 1 :(得分:0)

一如既往,我花了一整天的时间来修复它,并在发布问题后找到了解决方法。

好的,问题在申请的上述部分中没有出现。在我的资产管道中,我已经包含backbone_rails_sync.js,它允许将属性包装在paramRoot中,这样当您执行model.save()时,转到rails控制器的参数如下所示:

{"conference"=>{"title"=>"new conference", "category" => "super conferences"}, "action"=>"create", "controller"=>"conferences"}

而不是

{"title"=>"new conference", "category" => "super conferences", "action"=>"create", "controller"=>"conferences"}

这个特殊的同步补丁完全让我失望。

溶液:

经过不多考虑之后,我用不同的monkey patch 替换了它。现在一切正常,我的参数仍然包裹得很好。