【问题标题】:Best way to make a Backbone app with RESTful Slim Framework使用 RESTful Slim 框架制作 Backbone 应用程序的最佳方式
【发布时间】: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&iacute;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


【解决方案1】:

我在 RyanP13 的帖子中看到了答案。

所以固定的View代码是:

var ErrorList = Backbone.View.extend({
    el: '.page',
    render: function () {
        var that = this;
        var errores = new Errores();
        errores.fetch({
            success: function(errores, response, options){
                var template = _.template($('#error-list-template').html());
                that.$el.html(template({ errores: errores.toJSON()}));

            }
        });
    }
}); 

问题是我没有从集合中得到任何参考。

希望这对你也有帮助。

最好的问候

拉法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 2014-01-10
    相关资源
    最近更新 更多