2012-01-15 6 views
0

내가 장고 + Backbone.js를 실행하고 및 .save에 저장하지. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 이것은 내 백본 자바 스크립트 코드입니다. 우선 순위 목록을 구현하려고하는데 서버로 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를 오버라이드 (override)하는 경우

는 수동으로 그 메소드 내에서 저장 로직을 수행해야합니다.

그래서 하나는 Backbone.sync을 무시하거나 저장 수행하는 코드 내에서 아약스 호출을 추가하는 코드를 삭제합니다.

관련 문제