2012-01-15 7 views
0

Django + Backbone.jsを実行していて、.save()は効果がありません。私は間違って何をしていますか?これは私のバックボーンのJavaScriptコードです。私はリストを優先順位付けして実装しようとしており、サーバーにPOSTする方法を理解できません。私が試したときにChromiumは試みられた投稿を見ていません: T = new Task(); T.save();Backbone.jsがデータベースに保存されていません

コンソールで。

//$(function() { 

     /** 
     * Model: Task 
     * name, date, importance 
     */ 
     window.Task = Backbone.Model.extend({ 
     urlRoot: '/api/v1/task/', 

     initialize: function() { 
      console.log("New task: " + JSON.stringify(this.toJSON())); 
     } 

     , defaults: function() { 
      return { 
     date: new Date() 
     , name: "New event" 
     , importance: 0 
      }; 
     } 


     }); 

     /** 
     * Collections: Calendar 
     */ 

     window.Calendar = Backbone.Collection.extend({ 
      //urlRoot: '/api/v1/calendar', 

     initialize: function() { 
      console.log("New calendar: " + JSON.stringify(this.toJSON())); 
     } 

     , model: Task 

     , comparator: function(task) { 
      return task.get("date"); 
     } 

    /* 
     , before: function(thresholdDate) { 
      return this.filter(function(task) { 
     task.get('date') < thresholdDate; 
      }); 
     } 
    */ 


     }); 

     window.TaskView = Backbone.View.extend({ 

     tagName: "li" 



     }); 
    now = new Date(); 

    Day = Backbone.Collection.extend({ 
     model: Task, 
     url: '/api/v1/task/?format=json&calendar__id=1&date='+ now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate(), 
     parse: function(response) { 
     return response.objects; 
     }, 

     comparator: function(task){ 
     return task.get('priority');} 

    }); 

    Month = Backbone.Collection.extend({ 
     model: Task, 
     url: 'api/v1/task/?format=json&date__month='+(now.getMonth()+1), 
     parse: function(response){ 
     return response.objects; 
     }, 
     comparator: function(task){ 
     return task.get('priority');} 

     }); 


    Year = Backbone.Collection.extend({ 
     model: Task, 
     url: 'api/v1/task/?format=json&date__year='+now.getFullYear(), 
     parse: function(response){ 
     return response.objects; 
     }, 
     comparator: function(task){ 
     return task.get('priority');} 
     }); 




    // required for saving 
      Backbone.sync = function(method, model) { 
     console.log(method + ": " + JSON.stringify(model)); 
     model.id = 1; 
    }; 




    $.fn.serializeObject = function() 
    { 
     var o = {}; 
     var a = this.serializeArray(); 
     $.each(a, function() { 
     if (o[this.name] !== undefined) { 
      if (!o[this.name].push) { 
      o[this.name] = [o[this.name]]; 
      } 
      o[this.name].push(this.value || ''); 
     } else { 
      o[this.name] = this.value || ''; 
     } 
     }); 
     return o; 
    }; 

    $(function() { 
     $('form').submit(function() { 
     var dict = $('form').serializeObject(); 
     var new_task = new Backbone.Model({ 
     date: toString(dict.date), 
     name: toString(dict.name), 
     priority: toString(dict.priority)}); 
     console.log("new_task =" + new_task); 
     new_task.save(); 
     console.log(dict); 

     return false; 
     }); 

    }); 

    TaskView = Backbone.View.extend({ 
     el: $("div#app"), 
     render: function() { 
     $(thi.el).html(this.template(this.model.toJSON())); 
     } 
    });  
    //}); 
+0

詳細を私たちに伝えなければなりません。 – alexn

+0

'Backbone.sync'関数で保存ロジックを実装していないので保存されません。 – dfsq

+0

セーブロジックの適切な実装を参照できますか? – user784756

答えて

1

あなただけのコンソールメッセージをログに記録するBackbone.syncメソッドをオーバーライドしています。

Backbone.syncを上書きする場合、そのメソッド内で手動で保存ロジックを実行する必要があります。

したがって、Backbone.syncを上書きするコードを削除するか、そのコード内でajax呼び出しを追加して保存します。

関連する問題