Class JsModel
A single instance of MBX.JsModel
Defined in: js_model.js.
Constructor Attributes | Constructor Name and Description |
---|---|
JsModel(name, opts)
|
Field Attributes | Field Name and Description |
---|---|
class level attributes
|
|
events that this model will fire.
|
|
<static> |
JsModel.instanceMethods
add an instanceMethods attribute to the passed in attributes in order to extend
all instances of this model.
|
the model name of this model
|
Method Attributes | Method Name and Description |
---|---|
count()
Gives back the number of cached instances stored in this model
|
|
create(attrs)
Create an instance of the model
|
|
extendInstances(attrs)
use this to extend all instances of a single model
|
|
find(primaryKey)
find a single instance
|
|
findAll()
|
|
findByElement(el)
given a domElement with certain classes - return the instance that it belongs to
|
|
flush()
destroy all instances in the instance cache
|
|
get(key)
Use this to retreive attributes on a Model (rather than accessing them directly);
|
|
onAttributeChange(func)
A convenience method to subscribe to changing model attributes
|
|
onInstanceChange(func)
A convenience method to subscribe to changing model instances
|
|
onInstanceCreate(func)
A convenience method to subscribe to new model instances
|
|
onInstanceDestroy(func)
A convenience method to subscribe to destroying model instances
|
|
set(key, value)
Use this to set attributes of the model itself (rather than set them directly).
|
|
validateObject(instance)
this method to get extended later.
|
Class Detail
JsModel(name, opts)
- Parameters:
- name
- opts
- Throws:
- an error if there's no name, a name already exists or you specified a primaryKey and it wasn't a string
Field Detail
attributes
class level attributes
Event
events that this model will fire. Use this to hook into (at a very low level) events
MBX.EventHandler.subscribe(MBX.cssClass, MyModel.Event.newInstance, function (instance) { // dostuff } );
<static>
{Object}
JsModel.instanceMethods
add an instanceMethods attribute to the passed in attributes in order to extend
all instances of this model. You can also specify default attributes by adding
a defaults attribute to this attribute.
MyModel = MBX.JsModel.create("MyModel", { instanceMethods: { defaults: { myAttribute: "myDefault" }, myMethod: function (method) { return this.get('myAttribute'); } } }); MyModel.create().myMethod() == "myDefault";
{String}
modelName
the model name of this model
Method Detail
{number}
count()
Gives back the number of cached instances stored in this model
- Returns:
- {number} number of instances
create(attrs)
Create an instance of the model
MyModel = MBX.JsModel.create("MyModel"); var instance = MyModel.create({ myAttr: 'boo' }); instance.get('myAttr') == 'boo';
- Parameters:
- {Object} attrs
- attributes you want the new instance to have
- Throws:
- "trying to create an instance with the same primary key as another instance" if you are trying to create an instance that already exists
- Returns:
- JsModel#instance
extendInstances(attrs)
use this to extend all instances of a single model
- Parameters:
- {Object} attrs
- methods and attributes that you want to extend all instances with
find(primaryKey)
find a single instance
- Parameters:
- {String} primaryKey
- a JsModel#instance primaryKey
- Returns:
- an instance of this element
findAll()
- Returns:
- all instances of this model
findByElement(el)
given a domElement with certain classes - return the instance that it belongs to
- Parameters:
- {DomElement} el
- the element which has the correct classes on it
- Returns:
- an instance of this model or null
flush()
destroy all instances in the instance cache
get(key)
Use this to retreive attributes on a Model (rather than accessing them directly);
Model.set('myAttr', 'foo'); Model.get('myAttr', 'foo');
- Parameters:
- {String} key
- See:
- MBX.JsModel.set
onAttributeChange(func)
A convenience method to subscribe to changing model attributes
AModelInstance.onAttributeChange(function (evt) { console.dir(evt); });
- Parameters:
- func
onInstanceChange(func)
A convenience method to subscribe to changing model instances
AModelInstance.onInstanceChange(function (evt) { console.log(evt); });
- Parameters:
- func
onInstanceCreate(func)
A convenience method to subscribe to new model instances
AModelInstance.onInstanceCreate(function (evt) { console.log(evt) });
- Parameters:
- func
onInstanceDestroy(func)
A convenience method to subscribe to destroying model instances
AModelInstance.onInstanceDestroy(function (evt) { console.log(evt); });
- Parameters:
- func
set(key, value)
Use this to set attributes of the model itself (rather than set them directly).
It will automatically create events that will be relevant to controllers
Model.set('myAttr', 'foo'); Model.get('myAttr', 'foo');
- Parameters:
- {String} key
- the key of the attribute
- value
- the value to be assigned to the attribute
- See:
- MBX.JsModel.get
validateObject(instance)
this method to get extended later. Used mostly internally. Right now it only verifies
that a primaryKey is allowed to be used
- Parameters:
- {JsModel#instance} instance
- the instance that's being validated