Giter Club home page Giter Club logo

backbone-websql's Introduction

Backbone-WebSQL

Implementation of Backbone.sync to store data to WebSQL (available on webkit-based browsers).

How to run the tests

  • Start a server in the root ($ http-server -c-1)
  • Navigate to test/index.html (http://localhost:8080/test/index.html)

With polite thanks to Smarcoms web services s.r.o.

backbone-websql's People

Contributors

darrenhaken avatar josefadamcik avatar leahfitch avatar markmyoung avatar marrliss avatar prust avatar romanlv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

backbone-websql's Issues

Question: can i use foreign keys?

If i have a "complex" model, can i user foreign keys?

I am new to backbone and i have some doubdts of ow can i use this things.

Thanks

Error en update function

The update function has an error when creates the SQL sentence. The update SQL syntax is:

UPDATE table_reference
SET col_name1='expr1' , col_name2='expr2' 
WHERE where_condition

But in the update function is creating something like

UPDATE table_reference
SET col_name1='expr1' AND col_name2='expr2' 
WHERE where_condition

Notice the AND instead of a comma.

The fix is really easy, change setStmts.join(" AND ") in line 136 with setStmts.join(" ,").

Thank you.

Demo script

Hello,

thanks for creating the websql persistance adapter.

Any chance of a sample html page to see it in action ?

Thanks,
Alex

License?

Can you make the license explicit?

Enhancement submission?

Thank you so much for this library! It saved me from an insane deadline (for a few days anyway). I know very little about javascript (and knew nothing about backbone two months ago) and I can't tell you how much this backbone-websql helped me!

This isn't so much an issue, but more the fact that I have no idea how to contact the person responsible for writing and maintaining backbone-websql.

I wrote an app that used Backbone to communicate with a Web Service. This was based on a template Web Service client generated by NetBeans.

Then I had the need for offline mode that would store the values locally as well, so I had to make some changes to how the id is used so that there would be minimal difference between working offline and online.

The end result is that your model has a 'urlRoot' and the 'store' attributes.

   var buildingStore = new WebSQLStore(app.db, "building");
models.Building = models.LocalRemoteModel.extend({
    urlRoot: app.urlBase + "entity.building/",
    store: buildingStore,
    idAttribute: 'buildingSeq',
    defaults: {
        buildingName: "",
        buildingAddress: ""
    },
    toViewJson: function() {
        var result = this.toJSON(); // displayName property is used to render item in a list
        result.displayName = this.get('buildingName');
        return result;
    }
});

models.BuildingCollection = models.LocalRemoteCollection.extend({
    model: models.Building,
    store: buildingStore,
    url: app.urlBase + "entity.building/"
});

var shopStore = new WebSQLStore(app.db, "shops", [{name: 'fkeyBuildingSeq', type: 'number'}]); //f add column names

Other than that I wrote a base Model and Collection that looks at a setting to decide whether it's a local call or a remote one and then calls Backbone.sync for local or Backbone.originalSync for remote. For remote calls I wrap the parameters in $.param(options.data); so that the parameters in the fetch call can be normal javascript.

if (app.alwaysUseRemote == true || options.remoteSync) {
    //f if remote and there are parameters, use $.param() here, so we can get json object notation for local storage
    if (options.data) {
        options.data = $.param(options.data);
    }
    result = Backbone.originalSync(method, model, _.extend(options, errorHandler));
} else {
    //f use the options.data to get the params:  someFkey=1  param2=Value2 
    if (options.data) {
        options.filters = options.data; //f for query search
    }
    result = Backbone.sync(method, model, _.extend(options, errorHandler));
}

I also changed it so that you could have a column that represents a field of an embedded entity, like a Customer Model could have a field for membership.seqNo (which I reference like membership$seqNo inside backbone-websql). This makes searching for data simpler.

A filtered fetch that works for both web service and local query:

var shopCollection = new models.ShopCollection();
shopCollection.fetch({
    data: {fkeyBuildingSeq: parseInt($('#building').val())}, //f uses query param.
    success: function() {
        //f do something with the collection of shops that are linked to this building
    }
});

I also changed the isSingleResult to check if the fetch call was made from a collection or a single Model, which is now consistent with what would happen when querying the web service.

I changed the parameters for the success/error callbacks of the sync method to work with backbone 0.9.10.

Then I added automatic schema migration for when you change your model to include extra columns (or remove them). It will now recreate those tables with the new columns populated from the javascript object in the value column.

I don't know if anyone else would like to use these features, but it seems like a nice fit: read data remotely and store locally (or vice versa) by only changing one parameter between fetch and save. Other sections of your site don't even need to know if the data is local or remote.

I'd like to contribute my changes, but don't know who to send them to for review.

collection.fetch() results in Uncaught TypeError: Cannot read property 'undefined' of undefined

When I perform a fetch on a collection, this bit of code get hits (ln 135):

case "read":    (model.attributes[model.idAttribute] ? store.find(model,success,error) : store.findAll(model, success, error)); 

and results in:

Uncaught TypeError: Cannot read property 'undefined' of undefined 

Now as far as I can see the collection has no attributes, so when it looks for the model.idAttribute'th item it throws an error.

I fixed by:

case "read":    (((model.attributes)&&(model.attributes[model.idAttribute])) ? store.find(model,success,error) : store.findAll(model, success, error)); 

Should the collection object have an attributes property?

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.