私は Backbone が初めてで、CoffeeScript も初めてです。URL からデータ (プロパティ) を取得するコレクションがあります。私の homeView では、このデータをテンプレートに追加してループします。
<ul>
<% _.each(data, function (row) { %>
<li><%= row.get('name') %></li>
<% }); %>
</ul>
これはうまくいきます。
ただし、個々の行 (プロパティ) を表示する場合でも、同じコレクションを使用してモデル (id) の属性を変更し、コレクションで呼び出される URL を変更して、1 つのデータ (1 つのプロパティ) のみを取得します。 )。
私がそれをコーディングした方法は、私の個々のプロパティ ビューでは、コレクションをループし (行にしかありませんが)、メイン ビューに追加します。
class ShowPropertyView extends Backbone.View
constructor: ->
super
initialize: ->
@Property = new PropertyCollection #New collection
@Property.PropertiesModel.set({theID: @options.theID}) #Change URL and get one property with a specific ID
template: _.template($('#showProperty').html())
#propertyInfoTemplate: _.template($('#propertyInfoTemplate').html())
render: ->
#$(@.el).html(@template) #load main template
@loadResults() #load individual property
loadResults: ->
@Property.fetch({
success: (data) =>
$('#propertyInfo').append(@propertyInfoTemplate({data: data.models, _:_})) #Load data into another template & append template to main template
error: ->
alert('Unable to load information')
})
現在のテンプレート (データを受け取り、メイン テンプレートに追加される) は次のようになります (私の homeView テンプレートに似ています)。
<div>
<% _.each(data, function (row) { %>
<div>
<h3><%= row.get('name') %></h3>
</div>
<% }); %>
</div>
私が達成する必要があるのは、情報を 1 つのビューに渡し、アンダースコアのループ ステートメントの必要性を取り除き、これをメイン ビューに追加する必要がないことです (これは単なる個々のデータであるため)。
したがって、次のようなビューが 1 つだけあります。
<div>
<h3><%= row.get('name') %></h3>
</div>
いいえ、ShowPropertyView で何かを変更する必要があります。何がわからないのですか? どんな助けでも大歓迎です!ありがとう。