【发布时间】:2013-12-17 00:22:12
【问题描述】:
我需要一些帮助来从我的服务器获取数据并使用 Marionette 显示它。
在 Angular 中我会这样做:
index.html:
<body ng-app="app" ng-controller="AppCtrl as app">
<ul>
<li ng-repeat="person in app.people">
{{person.firstName}} {{person.lastName}}
</li>
</ul>
app.js:
var app;
app = angular.module("app", []);
app.controller("AppCtrl", function($http) {
app = this;
$http.get("http://localhost:3000/people").success(function(data) {
app.people = data;
});
});
我的服务器在我的服务器上(快递):
var people = [
{
id: 1,
firstName: 'Bob',
lastName: 'Blob',
phoneNumber: '123'
}, {
id: 2,
firstName: 'Valdemar',
lastName: 'Ugh',
phoneNumber: '456'
}
];
app.get('/people', function(req, res) {
res.send(people);
});
现在,我如何使用 Marionette 获得类似的结果?
我有一个模型(在这个例子中使用的是 coffeescript):
class Person extends Backbone.Model
我也有收藏:
class People extends Backbone.Collection
model: Person
url: '/people'
然后我会这样做:
people = new People
people.fetch
success: ->
console.log 'works ok'
return people
error: (data) ->
console.log 'no success'
console.log people
view = new Views model: people
我在 console.log 中得到的是:
works ok
People {length: 0, models: Array[0], _byId: Object, constructor: function, model: function…}
_byId: Object
length: 2
models: Array[2]
__proto__: ctor
现在,我的问题是如何使用它?为了简单地列出人,我什至需要这个集合还是只能用模型来做?我将如何 console.log 我列表中所有联系人的名字?为什么它显示 length: 0, models: Array[0] 却有 2 个模型??
【问题讨论】:
标签: angularjs rest backbone.js marionette