2012-07-31 68 views
1

我不得不代码:Backbone.js的不渲染JSON -

window.TicketCollection = Backbone.Collection.extend({ 
    model:Tickets, 
    url:"/index.php/tickets/viewJSON" 
}); 
window.TicketsView = Backbone.View.extend({ 
    tagName:'div', 
    initialize: function() { 
     this.model.bind("reset", this.render, this); 

    }, 
    render:function(eventName){ 
     _.each(this.model.models, function(ticket){ 
      $(this.el).append(new TicketListItemView({model:ticket}).render().el); 
     },this); 
     return this; 
    } 
}); 
window.TicketListView = Backbone.View.extend({ 
    tagName: "li", 
    template:_.template($('#tickets-list-item').html()), 
    render:function (eventName){ 
     $(this.el).html(this.template(this.model.toJSON)); 
     return this; 
    } 
}); 


var AppRouter = Backbone.Router.extend({ 
    routes:{ 
     "":"list" 
    }, 
    list:function(){ 
     window.alert("alright"); 
     this.ticketList = new TicketCollection(); 
     this.ticketLists = this.ticketList.get(); 

     this.ticketListView = new TicketListView({model:this.ticketList}); 
     $("#ticketListHolder").html(this.ticketListView.render().el); 
    }, 
}); 
var app = new AppRouter(); 
Backbone.history.start(); 
}); 

我的PHP如下:

<?php header('Content-type: application/json'); 
echo json_encode(array("ticketID"=>"test", "ticketName"=>"1"));?> 

从PHP的反应是:

[{"ticketID":"1","ticketName":"Fix the admin system"}] 

HTML :

<div id="ticketListHolder"> 
    # 


</div> 
<script type="text/template" id="tickets-list-item"> 
     <div class="supTicket ticketHolder nobg"> 
     <div class="issueType"> 
     <span class="issueInfo"><%= ticketName %></span> 
     <span class="issueNum">[ #<%= ticketID %>] - <%= fullName %></span> 
     </div> 
     </div> 
</script> 

我得到的错误:Uncaught ReferenceError: ticketName is not defined,它似乎不是解析json,而是将它读为一个字符串块。为什么这个错误发生,当我的JSON返回正确的数据。

+0

你的假设在哪里_“看起来它不是解析json,而是将它作为一个字符串块”_基于?“读取。我认为你需要调试一下,看看系统的状态不是预期的第一点,几个'console.log'在这里和那里可以帮助你。例如,_how的'ticket'看起来像这个块'_.each(this.model.models,function(ticket){..}'?_。并且检查你的_JS console_来检查_AJAX调用的_responses_。 – fguillen 2012-07-31 14:46:48

+0

AJAX调用响应良好,一个'控制台。log' on this.ticketList.fetch()'(参见@ nikoshr的答案)返回'responseText:“{”ticketID“:”test“,”ticketName“:”1“}'' – rickyduck 2012-07-31 14:50:36

+0

'ticket'看起来像什么样到这个块'_.each(this.model.models,function(ticket){..}'?我想看看'ticket'引用是我们期望的,并且它的_attributes_被正确填充。 – fguillen 2012-07-31 14:54:48

回答

1

您不要在任何地方使用collection.fetch。你的路由器也许应该是这样的

var AppRouter = Backbone.Router.extend({ 
    routes:{ 
     "":"list" 
    }, 
    list:function(){ 
     window.alert("alright"); 
     this.ticketList = new TicketCollection(); 

     this.ticketListView = new TicketListView({ 
       model:this.ticketList, 
       el:$("#ticketListHolder") // I directly assigned #ticketListHolder as the el 
     }); 

     this.ticketList.fetch(); 
    }, 
}); 

而且随着你的代码的主要工作版本小提琴http://jsfiddle.net/Cc9Ad/2/ 一些点,你应该检查:

  • 你的ListView和你ItemView控件是倒过来,
  • 丹尼尔阿兰达说,他的回答,尽量使用集合和模型为他们的预期目的,
  • this.model.toJSON应该已经this.model.toJSON()
  • 01您的模型个
  • 默认设置,全名是没有定义,如果在这种状态下

修改后的代码

window.Tickets=Backbone.Model.extend({ 
    defaults: { 
     fullName :"No full name" 
    } 
}); 
window.TicketCollection = Backbone.Collection.extend({ 
    model:Tickets, 
    url:"/index.php/tickets/viewJSON" 
}); 


window.TicketsView = Backbone.View.extend({ 
    tagName:'li', 
    template:_.template($('#tickets-list-item').html()), 

    initialize: function() { 
    }, 
    render:function(eventName){ 
     console.log 
     $(this.el).html(this.template(this.model.toJSON())); 
     return this; 
    } 
}); 
window.TicketListView = Backbone.View.extend({ 
    initialize: function() { 
     this.collection.bind("reset", this.render, this); 

    }, 

    render:function(){ 
     this.collection.each(function(ticket){ 
      $(this.el).append(new TicketsView({model:ticket}).render().el); 
     },this); 

     return this; 
    } 
}); 


     var ticketList = new TicketCollection(); 

     var ticketListView = new TicketListView({ 
       collection:ticketList, 
       el:$("#ticketListHolder") 
     }); 

// ticketList.fetch(); 
ticketList.reset([ 
    {"ticketID":"1","ticketName":"Fix the admin system"}, 
    {"ticketID":"2","ticketName":"The ticket 2"} 
]); 
+0

我没有收到此代码的错误,但是模板没有填充。这是一个单独的问题吗? – rickyduck 2012-07-31 14:30:46

+1

@rickyduck有一些在那里出现一些出错的东西。我会尽力为小提琴提供一个工作版本。 – nikoshr 2012-07-31 14:45:15

+0

@rickyduck我修改我的答案与小提琴 – nikoshr 2012-07-31 14:53:45

0

我使用JSON.parse()函数来解析PHP发送的数据。

var a = $.ajax({ 
    url:"/index.php/tickets/viewJSON", 
    async:false, 
    cache:false 
}).responseText; 

jsonData = JSON.parse(a); 


then use _.each to loop jsonData then push each data to your model. 
+0

如何使用_.each来循环? – rickyduck 2012-07-31 14:16:31

+1

如果您使用的是Backbone,没有任何需要使用JSON.parse函数,Backbone做它 – 2012-07-31 14:25:21

+0

就像你如何处理你的模型: _each(jsonData,function(ticket){ – 2012-07-31 14:46:25

1

型号不超过集合一样使用会破坏模板引擎。

您正在尝试使用Collection作为模型。

检查这个例子: http://danielarandaochoa.com/backboneexamples/blog/2012/02/22/real-world-usage-of-a-backbone-collection/

而且解决您的具体问题,传递到你的模板的对象,而不是一个Backbone.Model

$(this.el).html(this.template(this.model.toJSON())); 

你缺少括号。

但正如我所说,我建议你阅读文章解释如何使用集合。

+0

@rickyduck我仔细检查了你的代码,我认为你的问题是当你试图将你的模型转换为模板的JSON对象时缺少括号。 – 2012-07-31 14:29:18

+0

好的,这是一篇很棒的文章。现在将通读它。我用括号试了一下,没有什么区别。 – rickyduck 2012-07-31 14:30:24

+0

(抱歉顺便说一句 - Backbone对我来说是一件新事物,有点令人困惑) – rickyduck 2012-07-31 14:33:21