GithubHelp home page GithubHelp logo

miguelramosfdz / caching_alloy_restadapter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aaronksaunders/caching_alloy_restadapter

0.0 2.0 0.0 67 KB

A rest api adapter for appcelerator alloy that has built in caching

caching_alloy_restadapter's Introduction

Alloy REST API Adapter with built in caching support

A rest api adapter for appcelerator alloy that has built in caching

the caching code was refactored from this repo https://gist.github.com/johnthethird/560183

The REST adapter is one of the most used adpaters on projects that we build here a Clearly Innovative. Also integrating third party APIs with request limits and just for overall performance benefits we felt the need to cache data at various times.

Also we believe that the rest api for alloy should stay true to the original backbonejs model so we have not made any modifications to the why the adapter functions. It has been written so your code that interacts with alloy models can be used in a javascript application with the default backbone sync adapter.

Enjoy, if you have issues, please log them

How it works

Using the Cache

The settings can be added as configuration options when setting up you model. the adapter will use these values by default unless value is specified on the model or in the options on the sync adapter call

/**
 * Model used to perform a global search on the API
 */

exports.definition = {
    config : {
        adapter : {
            type : "restapi_c",
        },
        cacheSettings : {
            cacheSeconds : 3000,     // timeToLive default value
            pruneSeconds : 2520000,  // pruneSeconds default value anything this old is GONE!!
        }
    },
    //
    // THE MODEL
    extendModel : function(Model) {
        _.extend(Model.prototype, {
        });
        return Model;
    },
    //
    // THE COLLECTION
    extendCollection : function(Collection) {

        _.extend(Collection.prototype, {
        });
        return Collection;
    }
};

you would setup the call like this when using it with your model

var UserPhotos = Alloy.Collections.instance('UserPhotos');
UserPhotos.fetch({
    timeToLive : 3000,    // how long in ms for item to persist in cache
    ignoreCache : false,  // can ignore cache completely and make the api call always
    success : function(_collection, _response) {
        Ti.API.info('OUTPUT');
        Ti.API.info(JSON.stringify(_collection.models, null, 2));
    },
    error : function(_error, _response) {

    }
});

Using the Activity Window

you will need to provide a library for displaying loading activity; the library should support the following methods.

/* how long to wait before closing window */
activityWindow.setMaxTimeout();

/* show the window, with a message */ 
activityWindow.show([message to display]);

/* hide the window */
activityWindow.hide();

you would setup the call like this when using it with your model

var UserPhotos = Alloy.Collections.instance('UserPhotos');
UserPhotos.fetch({
    activityWindow : require('activityWindowLib'),
    activityWindowTimeout : 60000,
    success : function(_collection, _response) {
        Ti.API.info('OUTPUT');
        Ti.API.info(JSON.stringify(_collection.models, null, 2));
    },
    error : function(_error, _response) {

    }
});

you could also leverage the flexibility of javascript and do this

var UserPhotos = Alloy.Collections.instance('UserPhotos');

/* set the properties on the collection object */
UserPhotos.activityWindow = require('activityWindowLib');
UserPhotos.activityWindowTimeout = 60000;

/* fetch the objects for the collection */
UserPhotos.fetch({
    success : function(_collection, _response) {
        Ti.API.info('OUTPUT');
        Ti.API.info(JSON.stringify(_collection.models, null, 2));
    },
    error : function(_error, _response) {

    }
});

License

(The MIT License)

Copyright (c) 2014 Aaron Saunders <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

caching_alloy_restadapter's People

Contributors

aaronksaunders avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.