xavierlacot / joli.js

joli.js is an Activerecord-like javascript ORM, particularly suited for being used in the Appcelerator Titanium Mobile framework.
MIT License
267 stars 60 forks source link

Extending models broken? #3

Closed digitalscientists closed 13 years ago

digitalscientists commented 13 years ago

In my attempts model extensions defined in a model's methods are not reachable as defined in the readme, including using the human model example.

jefftriplett commented 13 years ago

I added a m = Object(); to fix it.

coomsie commented 13 years ago

mmm,

I can't seem to get this to work either, not sure what you did to sort it?

CODE BELOW FAILS

Titanium.include('joli.js');

joli.connection = new joli.Connection('joli_example');

var models = (function() { m = Object(); m.human = new joli.model({ table: 'human', columns: { id: 'INTEGER PRIMARY KEY AUTOINCREMENT', city_id: 'INTEGER', first_name: 'TEXT', last_name: 'TEXT' } , methods: { move: function(newCityName) { // search for the city id var city = joli.models.get('city').findOneBy('name', newCityName);

    if (!city) {
      throw 'Could not find a city with this name!';
    } else {
      this.city_id = city.id;
    }
  }
}

});

m.city = new joli.model({ table: 'city', columns: { id: 'INTEGER', country_id: 'INTEGER', name: 'TEXT', description: 'TEXT' } });

m.country = new joli.model({ table: 'country', columns: { id: 'INTEGER', name: 'TEXT' } });

m.searches = new joli.model({ table: 'searches', columns: { id: 'INTEGER PRIMARY KEY AUTOINCREMENT', name: 'TEXT', } });

return m; })();

joli.models.initialize();

///Inserting data can be done using the newRecord() method of a model:

// // create the record (not persisted) var john = models.human.newRecord({ first_name: 'John', last_name: 'Doe' });

// move him to New York john.move('New York');

// persist it john.save();

//other way

var john = new joli.record(models.human); john.fromArray({ first_name: 'John', last_name: 'Doe' });

// move him to New York john.move('New York');

// persist it john.save();

var q = new joli.query() .select('human.*') .from('human') .order(['last_name desc', 'first_name asc']);

// if (win.city_id) { // q.where('city_id = ?', win.city_id); // } // // if (win.last_name) { // q.where('last_name LIKE ?', '%' + win.last_name + '%'); // } // // if (win.city_name) { // q.where('city.name = ?', win.city_name); // q.join('city', 'city.id', 'human.city_id'); // }

var humans = q.execute();

Ti.API.info(humans);

rchandarana commented 13 years ago

I can't get it to work either. Any fixes available?

xavierlacot commented 13 years ago

Hi everybody,

Sorry for letting this bug for so long time. I have just pushed a fix, and adapted the documentation: https://github.com/xavierlacot/joli.js/commit/40ef6ecd633682ffdcb2b853b7dbbbe13b6da613