我有以下设置:如何处理不定值与骨干JSON数据木偶
App.module('TestUsers.Views', function(TestUsersViews, App, Backbone, Marionette, $, _) {
TestUsersViews.UsersItemView = Marionette.ItemView.extend({
template: testUsersItemViewTmpl
, tagName: 'tr'
, templateHelpers: {
handleUndefined: function(val) {
return (_.isUndefined(val) ? '' : val);
}
}
});
TestUsersViews.UsersTable = Marionette.CompositeView.extend({
template: testUsersTmpl
, tagName: 'table'
, className: 'h-center'
, itemView: TestUsersViews.UsersItemView
, itemViewContainer: 'tbody'
, initialize: function() {
this.listenTo(this.collection, 'reset', function() {
this.appendHtml = function(collectionView, itemView, index) {
collectionView.$el.append(itemView.el);
}
});
}
});
});
集合的结构返回的是:
[ { "apiStandardProfileRequest": { "headers": { "_total": 1, "values": [ { "name": "x-li-auth-token", "value": "name:ksBx" } ] }, "url": " http://api.linkedin.com/v1/people/jGEI3X15sx " }, "firstName": "Eileen", "headline": "Managing Director of Delivery at Kforce Professional Staffing", "id": "jGEI3X15sx", "industry": "Staffing and Recruiting", "lastName": "Adams (LION)", "location": { "country": { "code": "us" }, "name": "Greater Boston Area" }, "pictureUrl": " http://m.c.lnkd.licdn.com/mpr/mprx/0_y_g-snorc6G3qFIa- >bjSsz4yRb6un3EaOkWSszeCX3-yW5gmr5SOqvpuzEQPz6wGg8x2vtspSH8c", "siteStandardProfileRequest": { "url": " http://www.linkedin.com/profile/view ?>id=3633999&authType=name&authToken=ksBx&trk=api*a249733*s257591*" } },... ]
我的模板来呈现数据:
<td id="<%= id %>"><img src="<%= pictureUrl %>" width="16" height="16"/><%= firstName %> <%= lastName %></td>
<td><%= headline %></td>
<td></td>
<td><%= location.country.code %></td>
<td><%= location.name %></td>
<td><a href="<%= siteStandardProfileRequest.url %>">Full Profile</a></td>
然而,一些用户没有“pictureUrl”,我得到的“未捕获ReferenceErro错误r:pictureUrl未定义'。我不知道我在做什么错误,未定义的值没有处理。我相信这是一个简单的解决方法,任何帮助表示赞赏。
[这里](http://stackoverflow.com/questions/15283741/ignoring- undefined-data-vars-in-an-underscore-template)是一个关于下划线模板的未定义处理的相关问题。 – Mohit