Giter VIP home page Giter VIP logo

activejs-1584174's Introduction

ActiveRecord - Titanium Fork

This fork from the ActiveJS allows ActiveRecord support for Titanium Database.

ActiveJS originally supported different adapters up until commit a8cae9979b8caa01bc73. However, after this commit, the codebase was changed quite a bit, and the different adapters were removed. Hence this particular fork begins at commit 1584174, and is modified to support Titanium's Database interface.

I was trying to patch the latest tree but gave up because internally the latest ActiveJS code was changed a lot, especially the ActiveSupport namespace (methods are now separated into ActiveSupport.Object, ActiveSupport.Array, etc. instead of just exposed via ActiveSupport namespace ). I simply did not have the time to reverse-engineer all the changes to get everything working. Since I already had the commit 1584174 patched up nicely, I decided to go with this older version instead.

Android

This library has not been tested for the Android platform, in fact it's failing at the moment. You may get an error similar to this: (reported by Pete)

Location: [2692] in file active_record.js
Message: Too deep recursion while parsing

I will take a look into this issue in the near future.

Download & Usage

Copy the active_record.js to your Titanium project and include it in your code. You can now define the different models and their relationships. Please refer to the Titanium project for more information.

The Titanium Adapter

Since the iPhone's uses SQLite, the adapter was based on the gears.js adapter. However, Titanium.Database.DB.execute function is a wrapper on top of another Objective C's SQLite wrapper, it is particular quite picky and does not support the apply() method. In order for it to work, I had to resort to use eval() to construct the query params. Here's the code excerpt:

executeSQL: function executeSQL(sql)
{
  var args = ActiveSupport.arrayFrom(arguments);
  ActiveRecord.connection.log("Adapters.Titanium: " + sql + " [" + args.slice(1).join(',') + "]");

  var response;
  if( args.length == 1 ) { 
    response = ActiveRecord.connection.db.execute(sql);
  } else {
    args = args.slice(1);
    var params= [];
    for( var i = 0; i < args.length; i++ ) { 
      if( typeof(args[i]) != 'undefined' )
        params.push('args[' + i + ']');
      else
        params.push("''");
    }
    var statement = 'response = ActiveRecord.connection.db.execute(sql,' + params.join(',') + ')';
    // ActiveRecord.connection.log('Eval Statement: ' + statement);
    eval( statement );
  }
  return response;
},

Perfomance-wise the app will take a hit, but in exchange you'll have the power of ActiveRecord at your fingertips.

Test Coverage

Since the code can only work inside a Titanium's environment, the test suite for this fork is located under another Github repository, which is a Titanium project.

The tests are almost a direct copy/paste of the test cases under /text/active_record/. Most of them pass, except for the Finder's tests using callback.

Building

The files in the dist folder are the combined JSs, which can built using a rake task.

rake dist

You will need to have the gem sprocket installed (gem install sprocket).

About

This patch was done by Alex Le, single-founder of Marrily.com, an online wedding planning service.

Disclaimer

This code is provided as-is. Please use it at your own risk.

activejs-1584174's People

Contributors

sr3d avatar

Watchers

 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.