如何从钛中的骨干缓存响应更新UI

时间:2016-04-11 07:47:41

标签: javascript backbone.js titanium titanium-alloy

我正在尝试将缓存引入钛合金的骨干提取功能。我已经设法缓存响应并从缓存中获取对象,但是在Backbone方面仍未成功更新UI而没有向API发出请求。

我能够在网络浏览器上复制行为,但看起来钛合金上的情况有所不同。

以下是我的收藏/模型的代码,当 if(resp)有效时,精确度

exports.definition = {
config: {
    "URL": "http://example.com/best-sellers/",
    "adapter": {
        "type": "restapi",
        "collection_name": "best_sellers", 
        "idAttribute": "id"
    },
    "headers": { 
        "Accept": "application/json"
    },
},      
extendModel: function(Model) {      
    _.extend(Model.prototype, {});
    return Model;
},
extendCollection: function(Collection) {
    _.extend(Collection.prototype, {
        initialize: function() {
            this.cacheKey = "bestSellers";
            this.cacheTimeout = 1200;
        },
        fetch: function(options) {
            // Fetch resp from cache
            var resp = Ti.App.Cache.get(this.cacheKey);

            // Check if cache was not empty
            if (resp) {
                // Options if not set
                options = options || {};
                // Copy success method
                var success = options.success;

                options = _.extend({parse: true}, options);

                var collection = this;
                options.success = function(resp) {
                    var method = options.reset ? 'reset' : 'set';
                    collection['reset'](resp, options);
                    if (success) success.call(options.context, collection, resp, options);
                    collection.trigger('sync', collection, resp, options);
                };
                // ---> What should I be calling here to update the UI without doing a request to the API
            } else {

                // The cache object doesn't hold the required data
                // Preparing success method that set the cache 
                var success = options.success || function() {};
                var that = this;
                options.success = function(entity, resp, options) {
                    Ti.App.Cache.put(that.cacheKey, resp, this.cacheTimeout);
                    if (success) success(entity, resp, options);
                };
                // Calling the original fetch
                return Backbone.Collection.prototype.fetch.call(this, options);
            }
        }
    });
    return Collection;
}      

};

感谢您的帮助

0 个答案:

没有答案