2014-02-26 92 views
0

概述骨干不是JSON解析正确

我传递给我的骨架模型JSON对象。我在骨干模型中有一个解析函数来转换一些传入的属性。问题是当我获取这个模型时,这些属性没有被分析,只是添加到模型中。底部的图像显示,不是将密码转换为密码而是删除密码,它只是将密码添加到对象的属性中。

这里是我的代码:

JSON

当我使用邮递员打电话给我的web服务,我得到了响应:

{"type":null,"idTeacher":1,"name":"Sean","password":"tst","email":null,"dob":1392940800000} 

型号

window.Teacher = Backbone.Model.extend({ 
    urlRoot: "http://localhost:8080/SIMS/resource/teacher", 
    defaults: { 
      "id": null, 
     "Name": "", 
     "Password": "", 
     "email": "", 
     "dob": "", 
     "type": "" 

     }, 

      parse: function(response){ 
       response.id = response.idTeacher; 
       response.Password = response.password; 
       response.Name = response.name; 
       delete response.name; 
       delete resoponse.password; 
       delete response.idTeacher; 
       return response; 
      } 
}); 

window.TeacherCollection = Backbone.Collection.extend({ 
    model: Teacher, 
    url: "http://localhost:8080/SIMS/resource/teacher", 

     parse: function(response){ 

      return response; 

     } 

}); 

Main.js //这是

before: function(callback) { 
     if (this.teacherList) { 
      if (callback) callback(); 
     } else { 
      console.log('........................................javascript........'); 
      this.teacherList = new TeacherCollection(); 
      console.log('Loading List: Size: ' + this.teacherList.length); 
      this.teacherList.fetch({success: function() { 
       console.log('........... ftech success...........'); 
       $('#contents').html(new TeacherListView({model: app.teacherList}).render().el); 
       if (callback) callback(); 
      }}); 
     } 
    } 

如果调试我的骨干,我可以看到我的解析没有任何分析的变量和解析删除通话也不能工作。

enter image description here

UDATE ANSWER

感谢您的帮助。我没有收集类中的代码是一个问题。但第二个原因是我没有循环收集来改变每个属性。

回答

1

这是因为当您为集合调用fetch方法时,被调用的解析方法是集合的解析,而不是您的教师模型的解析。 当您从集合中调用获取方法时,集合期望收到一组模型,而不仅仅是一位老师,如您所述

1

您正在定义您的parse方法在您的Model,但调用您的Collectionfetch方法。

在这种情况下,只会调用您的Collectionparse方法。