0

バックボーンコレクションを取得しようとして少し立ち往生しています。私はバックボーンに不慣れで、簡単なものを見逃していると確信していますが、何がわかりません。

アイデアはありますか?

$ ->


User = Backbone.Model.extend(
    urlRoot: "/users"
    )
  user = new User()

  View = Backbone.View.extend(
    tagName: "li"
    el: "ul"
    id: "#users"
    template: _.template("<li>...just a code...<span class='buttons'><button class='info'>Info</button><button class='delete'>Delete</button></span></li>")
    render: -> 
      @$el.html(@template())
    )



view = new View(
    model: user
    )
  view.render().el

  Users = Backbone.Collection.extend(
    model: User
    url: "/users"
    parse: (resp, xhr) ->
      return resp.toJSON()
    )



usersCollection = new Users() 
  usersCollection.fetch(
    success: (response) ->
      console.log 'success'
      console.log usersCollection
      console.log response
    error: (data, response) ->
      console.log(response)
    )

私の "/users/ はこの配列を返します:

[{"created_at":"2013-05-25T18:00:43Z","email":"user1@mail.ru","id":1,"updated_at":"2013-05-25T18:00:43Z"},{"created_at":"2013-05-25T18:00:43Z","email":"user2@mail.ru","id":2,"updated_at":"2013-05-25T18:00:43Z"},{"created_at":"2013-05-25T18:00:43Z","email":"user3@mail.ru","id":3,"updated_at":"2013-05-25T18:00:43Z"},{"created_at":"2013-05-25T18:00:43Z","email":"user4@mail.ru","id":4,"updated_at":"2013-05-25T18:00:43Z"},{"created_at":"2013-05-25T18:00:43Z","email":"user5@mail.ru","id":5,"updated_at":"2013-05-25T18:00:43Z"},{"created_at":"2013-05-25T18:00:43Z","email":"user6@mail.ru","id":6,"updated_at":"2013-05-25T18:00:43Z"},{"created_at":"2013-05-25T18:00:43Z","email":"user7@mail.ru","id":7,"updated_at":"2013-05-25T18:00:43Z"}]

コントローラー (Ruby on Rails) は次のようになります。

  def index
    users = User.all
    render json: users, status: 200
  end

そしてconsole.logには次のものがあります:

success
child {length: 1, models: Array[1], _byId: Object, constructor: function, model: function…}
4

2 に答える 2

1

ビュー定義に初期化プロパティを追加し、コレクションがリセットされたときに「listenTo」を使用してイベントをオブジェクトにバインドしてみてください (例):

View = Backbone.View.extend(
    tagName: "li"
    el: "ul"
    initialize: function(){
        this.collection = new app.YourCollection();
        this.collection.fetch({ reset: true });

        this.listenTo(this.collection, 'reset', this.render);
    },

次に、render プロパティ (この場合はメソッド) で:

render: function(){
        this.collection.each(function(item){
            this.renderWeeklyMenu(item);
        }, this);
    },

renderItem: function(item){
        var itemView = new app.itemView({
            model : item
        });
        this.$el.append(itemView.render().el);
    }

また、「fetch」は JSON データを返すため、次の必要はありません。

parse: (resp, xhr) ->
      return resp.toJSON()
    )

お役に立てれば。

于 2016-10-31T10:45:02.827 に答える
0

parse応答はデフォルトでバックボーンが期待する形式であるため、関数を削除します。JSON を適切に解析し、配列をモデル インスタンスに変換されるデータ オブジェクトのコレクションとして扱います。

したがって、フェッチは機能していますが、適切ではありません A)コレクションまたはモデルからのデータをビューに含めます(したがって、それが機能しているかどうかを簡単に確認できず、B)によってロードされた後、データを使用してビューを再レンダリングしますコレクションのsyncイベントにバインドします。

于 2013-05-26T07:20:04.257 に答える