2012-03-20 3 views
1

に "IDが定義されていない" 私はSpring MVCの3JSPBackbone.js Wine Cellar Tutorial — Part 1: Getting Startedを使用してBACKBONE.JSをテストしています。 JSPには独自の<%= %>があるので、私はmain.jsの口ひげスタイルマーカを次のように宣言しました。underscore.js

_.templateSettings = { 
     interpolate : /\{\{(.+?)\}\}/g 
}; 

それに応じて、指定されたHTMLページを.jspページに変更して正しく動作させました。しかし、私はアプリケーションを実行すると、次のエラーが発生します。

enter image description here

続いて "index.htmlを-変更-に-のindex.jsp" ページです。

<%@ include file="/WEB-INF/views/include.jspf" %> 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Cellar</title> 
<%--Stylesheets --%> 
<link href="<c:url value="/resources/css/styles.css" />" rel="stylesheet" type="text/css" /> 
</head> 
<body> 
<div id="header"><span class="title">Backbone Cellar</span></div> 

<div id="sidebar"></div> 

<div id="content"> 
<h2>Welcome to Backbone Cellar</h2> 
<p> 
This is a sample application part of of three-part tutorial showing how to build a CRUD application with Backbone.js. 
</p> 
</div> 

<!-- Templates --> 
<script type="text/template" id="tpl-wine-list-item"> 
    <a href='#wines/{{ id }}'>{{ name }}</a> 
</script> 

<script type="text/template" id="tpl-wine-details"> 
    <div class="form-left-col"> 
     <label>Id:</label> 
     <input type="text" id="wineId" name="id" value="{{ id }}" disabled /> 
     <label>Name:</label> 
     <input type="text" id="name" name="name" value="{{ name }}" required/> 
     <label>Grapes:</label> 
     <input type="text" id="grapes" name="grapes" value="{{ grapes }}"/> 
     <label>Country:</label> 
     <input type="text" id="country" name="country" value="{{ country }}"/> 
     <label>Region:</label> 
     <input type="text" id="region" name="region" value="{{ region }}"/> 
     <label>Year:</label> 
     <input type="text" id="year" name="year" value="{{ year }}"/> 
    </div> 
    <div class="form-right-col"> 
     <img height="300" src="<c:url value='/resources/images/{{ picture }}' />" /> 
     <label>Notes:</label> 
     <textarea id="description" name="description">{{ description }}</textarea> 
    </div> 
</script> 

<%--JavaScripts --%> 
<script type="text/javascript" src="<c:url value="/resources/js/jquery-1.7.1.js" />"></script> 
<script type="text/javascript" src="<c:url value="/resources/js/underscore.js" />"></script> 
<script type="text/javascript" src="<c:url value="/resources/js/backbone.js" />"></script> 
<script type="text/javascript" src="<c:url value="/resources/js/main.js" />"></script> 
</body> 
</html> 

なぜこのエラーが発生するのか理解しやすくなりますか?

ありがとうございました。

EDIT:

main.js

// Using Mustache style markers 
_.templateSettings = { 
     interpolate : /\{\{(.+?)\}\}/g 
}; 

// Models 
window.Wine = Backbone.Model.extend(); 

window.WineCollection = Backbone.Collection.extend({ 
    model:Wine, 
    url:"/mavenedge/wines" 
}); 


//Views 
window.WineListView = Backbone.View.extend({ 

    tagName:'ul', 

    initialize:function() { 
     this.model.bind("reset", this.render, this); 
    }, 

    render:function (eventName) { 
     _.each(this.model.models, function (wine) { 
      $(this.el).append(new WineListItemView({model:wine}).render().el); 
     }, this); 
     return this; 
    } 

}); 


window.WineListItemView = Backbone.View.extend({ 

    tagName:"li", 

    template:_.template($('#tpl-wine-list-item').html()), 

    render:function (eventName) { 
     $(this.el).html(this.template(this.model.toJSON())); 
     return this; 
    } 

}); 

window.WineView = Backbone.View.extend({ 

    template:_.template($('#tpl-wine-details').html()), 

    render:function (eventName) { 
     $(this.el).html(this.template(this.model.toJSON())); 
     return this; 
    } 

}); 


// Router 
var AppRouter = Backbone.Router.extend({ 

    routes:{ 
     "":"list", 
     "wines/:id":"wineDetails" 
    }, 

    list:function() { 
     this.wineList = new WineCollection(); 
     this.wineListView = new WineListView({model:this.wineList}); 
     this.wineList.fetch(); 
     $('#sidebar').html(this.wineListView.render().el); 
    }, 

    wineDetails:function (id) { 
     this.wine = this.wineList.get(id); 
     this.wineView = new WineView({model:this.wine}); 
     $('#content').html(this.wineView.render().el); 
    } 
}); 

var app = new AppRouter(); 
Backbone.history.start(); 

JSONは多分あなたはidを持つオブジェクトが渡されていないように見えますサーバー enter image description here

+1

:画像が変更されました。 – skip

+2

この質問には多すぎるノイズがあるので、エラーを再現する簡単なコード例を作成することができます。どのようにエラーが発生したのかは、あなたが_id_属性**を持っていない_hash_でテンプレートを_feeding_していることです。 – fguillen

+0

あなたはあなたの摂食のデータを表示できますか?手動でモデルを追加してみてください(サーバーから引き出したり、ルートを使用しないでください)。 – Jack

答えて

1

あなたが投稿したコードを使用して、モデルを作成する前にフェッチしているデータを調べてみてください。手動でモデルを作成して追加しても問題ないと思われる場合は、テンプレートに定義されているすべてのフィールドの値を取得していることを確認してください(@ fguillenがIDフィールドを見つけられていない可能性が高いと言われています)。

http://jsfiddle.net/khfCr/

編集:申し訳ありませんが、私はあなたが戻っJSONの投稿のスクリーンショットを見ることができなかった、それが正しい見えます。問題はおそらくデータがテンプレートに渡されている可能性が高いため、コレクション/モデルが正しく初期化されていない可能性があります。

EDIT2: 私は手動でリストを作成する代わりに、サーバとの例から、それを引っ張っすることは正常に動作しますが、それは正しく作成されているかどうかを確認するために、それをレンダリングする前に、あなたのコレクションを調べフィドルを更新)

http://jsfiddle.net/khfCr/1/

+0

'id'属性が' .jsp'ページで '

0

から返さプロパティを次のようにtemplate()呼び出しに追加します。

this.template({id:100, name:"Gareth"}); 
+0

私は何を渡しているかを示す質問を編集しました。 – skip

+1

テンプレートを実行しているポイントでデバッグして、model.toJSON()がワインオブジェクトであるかどうかを確認します。これにより、投稿したコードの残りの部分が除外されます。また、私はあなたのワインリストビューにコレクションを渡します。 this.wineListView =新しいWineListView({collection:this.wineList});. _each()が動作していない可能性があります –

1

cellar.jsp

<%@ include file="/WEB-INF/views/include.jspf" %> 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
    <title>Welcome to Backbone Wine Cellar</title> 

    <%--Templates --%> 
    <script type="text/template" id="tpl-wine-list-item"> 
     <a href="#">{{ name }}</a> 
    </script> 
</head> 
<body> 

    <%-- Javascript --%> 
    <script type="text/javascript" src="<c:url value="/resources/js/jquery-1.7.1.js" />"></script> 
    <script type="text/javascript" src="<c:url value="/resources/js/underscore.js" />"></script> 
    <script type="text/javascript" src="<c:url value="/resources/js/backbone.js" />"></script> 
    <script type="text/javascript" src="<c:url value="/resources/js/cellar.js" />"></script> 
</body> 
</html> 

CELLER。JS

//for using 'Mustache.js' style templating 
_.templateSettings = { 
    interpolate : /\{\{(.+?)\}\}/g 
}; 

(function(){ 
    var Wine = Backbone.Model.extend(); 

    var WineList = Backbone.Collection.extend({ 
     model: Wine, 
     url: "/mavenedge/wines", 
     parse: function(data) { 
      return data.wines; 
     } 
    }); 

    var WineListView = Backbone.View.extend({ 
     el: $("body"), 
     template: _.template($("#tpl-wine-list-item").html()), 
     initialize: function() { 
      _.bindAll(this, "render"); 
      this.collection = new WineList(); 
      this.collection.fetch(); 
      this.collection.bind("reset", this.render); //'reset' event fires when the collection has finished receiving data from the server 
     }, 
     render: function() {  
      _.each(
         this.collection.models, 
         function(wine) { 
          $(this.el).append(this.template({name: wine.get("name")})); 
         }, 
         this 
        ); 
     } 
    }); 

    var wineListView = new WineListView(); 
})(); 

返しMДΓΓБДLL&BalusC @

enter image description here

関連する問題