collection.create(model) で動作するように wait: true オプションを取得しようとしていますが、運がありません。
追加時にレンダリングされるビュー
window.ObjectsView = GreyboxView.extend
initialize: function() {
_.bindAll(this, 'render');
this.collection.bind('add', this.render);
return this.collection.bind('reset', this.render);
},
render: function() {
json = { obj removed }
this.template = _.template(JST['irboards/index'](json));
this.$el.html(this.template());
this.renderObjects();
return this;
}
}
モデル保存について (新規)
window.FormView = GreyboxView.extend({
// un-needed code omitted
saveModel: function() {
this.model.set { all my attributes }
this.collection.create(this.model, {
wait: true
});
}
}
レールコントローラーで:
def create
@model = Model.new(params[:model].merge({:account_id => current_user.account.id, :action_user_id => current_user.id}))
if @model.save
flash[:success] = AppSystem::Notifications.created('Model')
respond_with @model.to_json
else
flash[:error] = @model.errors[:base].blank? ? AppSystem::Notifications::FORM_ERROR : @model.errors[:base]
render :action => :new
end
end
基本的に {wait: true} は何もしません... js モデルがコレクションに追加されていないか、b/c がデータベースに保存されているか、
this.collection.bind('add', this.render)
がトリガーされていないか、レールが成功を返していない可能性があります...よくわかりません。
誰でも助けることができますか?
前もって感謝します
どうやら自分の質問にはまだ答えられないようですが、この b/c を忘れたくありません。他の人の助けになることはわかっています。それで:
うーん...長い間検索し、最終的にこの質問を投稿した後...これを処理する方法を発見しました。この投稿/ブログを見つけました
http://italktoomuch.com/2012/05/setting-up-backbonejs-with-ruby-on-rails/
基本的に私の create :action は
def create
respond_with = Model.create(params0
end
jsは次のとおりです。
this.collection.create(attributes, {
wait: false,
success: function() {
return _this.collection.trigger('reset');
},
error: function() {
return alert('wrong!');
}
})
それで、待っても何も起こらないと信じるしかありませんか?または私はそれを間違って使用していますか?わかりませんが、これは機能しており、満足しています。もう少しリファクタリングを行う必要がありますが、これは開始するのに適した場所です
これが他の人にも役立つことを願っています