Permalink
Browse files

Move from Neuro.

  • Loading branch information...
1 parent 007ed3f commit 062b088f883aebeef7fe7a196148030f39008eb5 @GCheung55 committed Jun 28, 2012
View
@@ -32,3 +32,5 @@
ehthumbs.db
Icon?
Thumbs.db
+
+node_modules
View
@@ -1,3 +0,0 @@
-[submodule "lib/class-extras"]
- path = lib/class-extras
- url = https://github.com/cpojer/mootools-class-extras.git
View
117 README.md
@@ -1,117 +0,0 @@
-Neuro
-======================
-
-A MooTools client-side MVC.
-
-### Version: 0.1.2 (Alpha)
-
-### Influences:
-
-* [Backbone](documentcloud/backbone)
-* [Shipyard](seanmonstar/Shipyard)
-
-### Dependencies:
-
-* [MooTools-Core 1.x](mootools/mootools-core)
-* [Is.js](gcheung55/is.js)
-
-### Focus:
-
-* uncoupled coding but allow for a coupled organization if necessary
-* provide base for applications
-
-Usage
------
-
-### Model
-
-Subclass Model with MooTools Class.
-
-```javascript
-var HumanModel = new Class({
- Extends: Neuro.Model
- // Set the default data
- _data: {
- firstName: ''
- ,lastName: ''
- ,hp: 10
- ,max: 100
- ,lvl: 1
- // You can set a function as a custom getter. "this" will be _data, not the model itself.
- ,name: function(){
- return this.firstName + ' ' + this.lastName;
- }
- }
-});
-```
-Create a model instance
-
-```javascript
-var bruceLee = new HumanModel({
- firstName: 'Bruce'
- ,lastName: 'Lee'
- ,hp: 1000
- ,lvl: 99
-});
-
-bruceLee.get('name'); // 'Bruce Lee'
-bruceLee.get('lvl'); // 99
-bruceLee.set('lvl', 100).get('lvl'); // 100
-```
-
-### Collection
-
-Subclass Collection with MooTools Class.
-
-```javascript
-var Humans = new Class({
- Extends: Neuro.Collection
-
- ,Model: HumanModel
-});
-```
-Add data to the collection
-
-```javascript
-// Add one dataset to the collection
-Humans.add({
- firstName: 'Chuck'
- ,lastName: 'Norris'
- ,hp: 1000
-});
-
-// Add multiple datasets to the collection
-Humans.add({
- firstName: 'Kareem Abdul'
- ,lastName: 'Jabbar'
- ,hp: 1000
- ,lvl: 80
-}, {
- firstName: 'Gary'
- ,lastName: 'Elms'
- ,hp: 800
- ,lvl: 81
-});
-
-// Add a model instance
-Humans.add(bruceLee);
-```
-Get a model from the collection by index
-
-```javascript
-Humans.get(4); // bruceLee model
-```
-Remove a model from the collection
-```javascript
-var garyElms = Humans.get(3); // Gary Elms model
-Humans.remove(garyElms);
-
-// Check if the removed model still exists in the collection
-Humans.hasModel(garyElms); // false
-```
-
-ToDo
-----
-* Add a builder
-* Add a Router mechanism
-* Add a Sync mechanism
View
@@ -1,223 +1,60 @@
-// (function(context){
-
-var Model = require('./Model'),
- Silence = require('../mixins/silence');
+/**
+ * Sync Collection
+ * Inspired by Epitome.Model.Sync by Dimitar Christoff (https://github.com/DimitarChristoff/Epitome)
+ *
+ * @type {Class}
+ *
+ * @todo Need to setup the url to be like /path/:id. This would allow plugin in data to the url
+ * Then set it to the request options url property
+ *
+ *
+ */
+
+var Neuro = require('Neuro'),
+ Sync = require('./Sync'),
+ Mixins = require('../mixins/sync');
var Collection = new Class({
- Implements: [Events, Options, Silence],
-
- _models: [],
+ Extends: Neuro.Collection,
- _bound: {},
+ Implements: [Mixins.Sync],
options: {
- // onAdd: function(){},
- // onRemove: function(){},
- // onEmpty: function(){},
- Model: Model,
- // Model Options
- modelOptions: undefined,
- silent: false
- },
-
- initialize: function(models, options){
- this.setup(models, options);
+ request: {},
+ Model: Neuro.Model
},
setup: function(models, options){
- this.setOptions(options);
-
- this._bound = {
- remove: this.remove.bind(this)
- };
-
- this._Model = this.options.Model;
-
- // Silent property determines whether model will excute signals
- this.silence(this.options.silent);
-
- if (models) {
- this.add(models);
- }
-
- return this;
- },
-
- hasModel: function(model){
- return this._models.contains(model);
- },
-
- /**
- * Private add method
- * @param {Class} model A Model instance
- * @return {Class} Collection Instance
- */
- _add: function(model){
- model = new this._Model(model, this.options.modelOptions);
-
- if (!this.hasModel(model)) {
-
- // Remove the model if it destroys itself.
- model.addEvent('destroy', this._bound.remove);
-
- this._models.push(model);
-
- this.signalAdd(model);
- }
-
- return this;
- },
-
- /**
- * Add a model or models
- * @param {Class || Array} A single Model instance or an array of Model instances
- * @return {Class} Collection Instance
- *
- * @example
- * collectionInstance.add(model);
- * collectionInstance.add([model, model]);
- */
- add: function(models){
- models = Array.from(models);
-
- var len = models.length,
- i = 0;
-
- while(len--){
- this._add(models[i++]);
- }
-
- return this;
- },
-
- /**
- * Get model by index
- * Overloaded to return an array of models if more than one 'index'
- * argument is passed
- *
- * @param {Number} index Index of model to return
- * @return {Class || Array} Model instance or Array of Model instances
- */
- get: function(index){
- var len = arguments.length, i = 0, results;
+ this.parent(models, options);
- if (len > 1) {
- results = [];
-
- while(len--){
- results.push(this.get(arguments[i++]));
- }
-
- return results;
- }
-
- return this._models[index];
- },
-
- /**
- * Private remove method
- * @param {Class} model A Model instance
- * @return {Class} Collection Instance
- */
- _remove: function(model){
- // Clean up when removing so that it doesn't try removing itself from the collection
- model.removeEvent('destroy', this._bound.remove);
-
- this._models.erase(model);
-
- this.signalRemove(model);
-
- return this;
+ this.setSync();
},
- /**
- * Remove a model or models
- * @param {Class || Array} A single Model instance or an array of Model instances
- * @return {Class} Collection Instance
- *
- * @example
- * collectionInstance.remove(model);
- * collectionInstance.remove([model, model]);
- */
- remove: function(models){
- // Cloning after converting to an Array because it should be dereferenced
- // in order to continue the while loop when erasing from this._models
- models = Array.from(models).slice();
-
- var l = models.length,
- i = 0;
-
- while(l--){
- this._remove(models[i++]);
+ _syncFetch: function(response, callback, reset){
+ // If data returns, set it
+ if (response) {
+ reset && this.empty();
+ this.add(this.parse.apply(this, response));
}
- return this;
- },
-
- /**
- * Replace an existing model with a new one
- * @param {Class} oldModel A Model instance that will be replaced with the new
- * @param {Object || Class} newModel An object or Model instance that will replace the old
- * @param {Boolean} signal A switch to signal add and remove event listeners
- * @return {Class} Collection Instance
- */
- replace: function(oldModel, newModel, signal){
- var index;
-
- if (oldModel && newModel) {
- index = this.indexOf(oldModel);
-
- if (index > -1) {
- newModel = new this._Model(newModel, this.options.modelOptions);
+ this.fireEvent('fetch', response);
- this._models.splice(index, 1, newModel);
-
- if (signal) {
- this.signalAdd(newModel);
-
- this.signalRemove(oldModel);
- }
- }
- }
+ callback && callback.call(this, response);
return this;
},
- empty: function(){
- this.remove(this._models);
+ fetch: function(callback, reset){
+ var data = this.toJSON();
- this.signalEmpty();
+ // Issue read command to server
+ this.sync('read', data, function(response){
+ this._syncFetch(response, callback, reset);
+ this.fireEvent('read', arguments)
+ });
return this;
- },
-
- signalAdd: function(model){
- !this.isSilent() && this.fireEvent('add', model);
- return this;
- },
-
- signalRemove: function(model){
- !this.isSilent() && this.fireEvent('remove', model);
- return this;
- },
-
- signalEmpty: function(){
- !this.isSilent() && this.fireEvent('empty');
- return this;
- },
-
- toJSON: function(){
- return this.map(function(model){
- return model.toJSON();
- });
}
});
-['forEach', 'each', 'invoke', 'every', 'filter', 'clean', 'indexOf', 'map', 'some', 'associate', 'link', 'contains', 'getLast', 'getRandom', 'flatten', 'pick'].each(function(method){
- Collection.implement(method, function(){
- return Array.prototype[method].apply(this._models, arguments);
- });
-});
-
-module.exports = Collection;
-// }(typeof exports != 'undefined' ? exports : window));
+module.exports = Collection;
Oops, something went wrong.

0 comments on commit 062b088

Please sign in to comment.