【发布时间】:2014-09-05 11:11:44
【问题描述】:
我正在使用此视频示例 (https://www.youtube.com/watch?v=FZSjvWtUxYk) 来制作错误报告应用。当视图中传递来自 REST-AJAX 调用的对象时,问题就出现了,_.template 说没有定义错误。
这是我的代码:
var Error = Backbone.Model.extend({
defaults: {
iderror: '',
title: '',
url: ''
},
idAttribute: 'iderror'
});
var Errores = Backbone.Collection.extend({
model: Error,
url: '/errores'
});
var ErrorList = Backbone.View.extend({
el: '.page',
render: function (eventName) {
var that = this;
var errores = new Errores();
errores.fetch({
success: function (errores) {
var template = _.template($('#error-list-template').html(),{errores: errores.models});
that.$el.html(template);
}
});
}
});
我的模板代码:
<script type="text/template" id="error-list-template">
<a href="#/new" class="btn btn-primary">Registrar Error</a>
<hr/>
<table class="table striped">
<thead>
<tr>
<th>Id Error</th>
<th>Título</th>
<th>URL</th>
<th></th>
</tr>
</thead>
<tbody>
<%
_.each(errores, function(error) { %>
<tr>
<td><%= error.get('iderror') %></td>
<td><%= error.get('title') %></td>
<td><%= error.get('url') %></td>
<td><a href="#/edit/<%= error.iderror %>" class="btn">Editar</a></td>
</tr>
<% }); %>
</tbody>
</table>
</script>
我使用 SLIM 框架制作的 RESTful GET 函数:
$app->get('/errores/', function () {
//echo "Hello, ";
$datos = array();
$db = NewADOConnection('mysqli');
$db->Connect("localhost", "root", "mypassword", "tt1r");
$sql = "SELECT * FROM tt1r.course";
$qry = $db->execute($sql);
/*while (!$qry->EOF) {
//print_r($qry->fields);
$datos = [$qry->iderror => ['title' => $qry->title, 'url' => $qry->url]];
$qry->MoveNext();
}*/
if($qry) {
foreach($qry as $re){
//return $re['ADM_ID'];
$datos[] = array("iderror" => $re['iderror'],"title" => $re['title'],"url" => $re['url']);
}
$db = null;
//echo json_encode($datos);
//echo '{"user": ' . json_encode($datos) . '}';
echo json_encode($datos);
}
});
我不得不说我确实从 RESTful 函数中获取了带有数据的 JSON 数组,但它没有显示出来。
感谢您的帮助。
最好的问候。
拉法
【问题讨论】:
-
阅读好像没问题... :S
-
是的,看起来像,但在我添加errores.toJSON()之前它对我不起作用
标签: rest templates backbone.js underscore.js slim