【问题标题】:How to render jsonp fetched in BackboneJS?如何在 Backbone JS 中渲染 json 获取?
【发布时间】:2014-02-22 16:42:39
【问题描述】:

我是 BackboneJS 的新手,我无法从 JSONP 呈现信息。如果我将数据放入 data.json 并获取它,则计数会出现在控制台中,但是当我使用 JSONP 时永远不会重新渲染。 我不知道获取数据是否存在某种延迟,但是集合没有触发“更改”和“重置”事件以重新渲染视图。 我的代码是下一个:

// 收藏

define([
'underscore',
'backbone',
'models/EstablecimientoModel'],function(_, Backbone, EstablecimientoModel){

var EstablecimientoCollection = Backbone.Collection.extend({
    model: EstablecimientoModel,

    initialize: function(models, options) {
        console.log("Establecimiento initialize");
    },

    url: function() {
        return '../js/establecimientos.json';  
        //return 'http://localhost:3000/establecimiento';
    },

    parse: function(data) {
        console.log("End of loading data " + JSON.stringify(data) + " datos");
        return data;   
    },
});

return EstablecimientoCollection;

});

// 路由器

define([
    'jquery',
    'underscore',
    'backbone',
    'views/establecimiento/EstablecimientoView',
    'jqm'
], function($, _, Backbone,EstablecimientoView) {
    'use strict';
    var Router = Backbone.Router.extend({
        //definition of routes
        routes: {
            'nearMe' : 'nearMe',
        },
        nearMe: function(actions) {
            var estaColl = new EstablecimientoCollection();

            var establecimientoView = new EstablecimientoView({ collection: estaColl });
            //estaColl.fetch();
            //establecimientoView.render();
            this.changePage(establecimientoView);
        },
        init: true,
        changePage: function(view) {
            //add the attribute data-role="page" for each view's div
            $(view.el).attr('data-role','page');
            view.render();
            // append to the DOM
            $('body').append($(view.el));
            var transition  = $.mobile.defaultPageTransition;

            if(this.firstPage) {
                transition = 'none';
                this.firstPage = false;
            }

            // Remove page from DOM when it’s being replaced 
            $('div[data-role="page"]').on('pagehide', function (event, ui) { 
                $(this).remove();
            });

            $.mobile.changePage($(view.el), { transition: transition, changeHash: false });   

        } // end of changePage()
    });

    return Router;
});

// 查看

define([
    'jquery',
    'underscore',
    'backbone',
    'collections/EstablecimientoCollection',
    'text!templates/establecimiento/establecimientoTemplate.html'
],function($, _, Backbone, EstablecimientoCollection, establecimientoTemplate) {

    var EstablecimientoView = Backbone.View.extend({
        initialize: function() {
            var self = this;

            _.bindAll(this,"render");
            this.collection.on("change",self.render);
            this.collection.fetch({ dataType: 'jsonp', success: function(){ self.render() }});

        }, //end of initialize()

        template: _.template(establecimientoTemplate),        

        render: function(eventName) {
            console.log("render");
            console.log(this.collection.length);

            return this;
        }, //end of render()
    });

    return EstablecimientoView;
});

【问题讨论】:

    标签: json jquery-mobile backbone.js jsonp


    【解决方案1】:

    当您获取数据时,请确保为您的 fetch 调用设置 dataType。 Fetch 包装了一个 jQuery/Zepto ajax 调用,因此您需要设置与这些参数相同的参数。

    this.collection.fetch({
        reset: true,
        dataType: 'jsonp',
        success: function () {
            // do stuff
        }
    });
    

    另外,我会让你的视图监听集合发布的事件,而不是直接从 fetch 的成功回调中调用视图的渲染。

    【讨论】:

    • 对不起,我忘记写dataType: 'jsonp'的部分,但我在测试中使用它并且不起作用。另一件事是我有这行:this.collecton.on("change",self.render);,但它没有触发事件,或者我做的参考不好?
    • 检查您的浏览器开发工具的网络选项卡,并确保请求是 A. 正在发出并且 B. 有一个看起来像有效 jsonp 的响应(即它被包装在一个函数调用中)。至于连接收集事件,这可能不是您想要的方式。通常,您希望在视图的初始化代码中为您的集合的重置事件添加一个侦听器:this.listenTo(this.collection, 'reset', this.render);
    • 还是不行。使用 JSONP 我可以观察请求和响应,并真正接收到数据。使用listenTo方法观察您所说的内容,我将此行添加到初始化this.listenTo(this.collection,'reset',this.render); this.collection.fetch();,而不是使用file.json制作JSONP,并且我在控制台中看到方法“render”不再被调用时集合已获取。
    • 您是否看到传递给您的解析方法的数据?
    • 好吧,我做了一些(gist.github.com/michielvaneerd/5989839),我发现“错误”的答案是“成功”,我发现我没有从服务器发送 JSONP,现在我接收数据但 this.listenTo(this.collection, 'reset',this.render) 永远不会触发,所以我必须这样做:this.collection.fetch({ success: function(){ self.render() } });。好不好用??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多