src/modules/o_O.model.js in eyeballs-0.4.2 vs src/modules/o_O.model.js in eyeballs-0.5.0

- old
+ new

@@ -2,11 +2,12 @@ adapter: false, initialize: function(model_name, callback){ var callback = callback; var class_methods, instance_methods, initializer_methods; var validates_presence_of, validates_length_of; - var table_name = model_name.toLowerCase() + 's'; + var table_name = model_name.underscore() + 's'; + var initial_class_methods = []; class_methods = { validations: {presence: [], lengthliness: [], custom: []}, methods: {}, validates_presence_of: function(field){ @@ -18,20 +19,25 @@ }, validates: function(validation){ this.validations.custom.push(validation) } } + for(var method in class_methods) + { + initial_class_methods.push(method); + } + - var run_callback = function(callback, method, args){ + var run_callback = function(callback, method, object, response){ try{ if(typeof callback === 'function' && method === 'success') { - callback(args); + callback(object, response); } if(typeof callback === 'object' && typeof callback[method] === 'function') { - callback[method](args); + callback[method](object, response); } } catch(e) { if(typeof console === 'object') @@ -50,13 +56,13 @@ adapter: o_O.model.adapter, destroy: function(callback){ run_callback(callback, 'loading', this) if(this.adapter) { - this.adapter.destroy(this, function(returned_object){ - run_callback(callback, 'success', returned_object); - }); + this.adapter.destroy(this, function(returned_object, response){ + run_callback(callback, 'success', returned_object, response); + }, callback); } else { run_callback(callback, 'success', this) } @@ -68,15 +74,15 @@ { run_callback(callback, 'loading', this); if(this.adapter) { var model = this; - this.adapter.save(this, function(returned_object){ + this.adapter.save(this, function(returned_object, response){ var initialized_object = o_O.models[model.model_name].initialize(returned_object); initialized_object.new_record = false; - run_callback(callback, 'success', initialized_object); - }); + run_callback(callback, 'success', initialized_object, response); + }, callback); } else { run_callback(callback, 'success', this); } @@ -105,11 +111,11 @@ this[attribute] = attributes[attribute]; } this.save(callback); }, valid: function(){ - this.errors = []; + this.errors.length = 0; o_O.validations.run(this); if(this.errors.length == 0) { @@ -121,10 +127,20 @@ } }, errors: [], validations: class_methods.validations } + instance_methods.errors.on = function(field){ + for(var i=0; i<instance_methods.errors.length; i++) + { + if(field === instance_methods.errors[i].field) + { + return instance_methods.errors[i]; + } + } + } + for(method in class_methods.methods) { instance_methods[method] = class_methods.methods[method] } @@ -155,10 +171,13 @@ { attributes['new_record'] = true; } return attributes; }, + create: function(attributes, callbacks){ + this.initialize(attributes).save(callbacks); + }, find: function(id, callback){ if(this.adapter) { run_callback(callback, 'loading', this) var model = this; @@ -179,9 +198,17 @@ }, callback); } }, model_name: model_name, table_name: table_name + } + + for(var method in class_methods) + { + if(initial_class_methods.indexOf(method) === -1) + { + initializer_methods[method] = class_methods[method]; + } } return initializer_methods; } } \ No newline at end of file