The id attribute on the viewModel. This is used for syncing with databases. Defaults to id
.
ko.Model.extend({
name: 'friend',
options: {
idAttribute: '_id'
}
});
The storage type to use with the model. If the model is inside a collection, the model will inherit the storage type from the collection.
ko.Model.extend({
name: 'friend',
options: {
storage: 'localStorage'
}
});
Current storage types include:
This makes the model automatically sync with the backend anytime there is a change. To turn autoSync on, pass in true
:
dog.autoSync(true);
Removes all of the data from the observables in the model:
dog.clear();
Returns one of the observable in the model:
dog.get('firstName'); // Fido
Returns the id attribute of the observables. By default, the ID attribute is an observable named "id", but this can be overridden using the "idAttribute" option.
var Dog = ko.Model.extend({
observables: {
id: 111
}
});
var fido = new Dog();
console.log(fido.getId()); // 111
If the requested observable returns a truthy value, this will be true:
fido.set('firstName', 'Fido');
console.log(fido.has('firstName')); // true
fido.set('firstName', null);
console.log(fido.has('firstName')); // false
Returns the JSON values for the observables in the model.
Inserts new data into the database.
var Model = ko.Model.extend({
name: 'friend',
observables: {
firstName: '',
lastName: '',
id: null
}
});
var model = new Model();
model.insert({ firstName: 'Milo', lastName: 'Cadenhead' }, function (data) {
console.log(data);
// { firstName: 'Milo', lastName: 'Cadenhead', id: 1382539084406 }
});
Updates the data in the database.
var Model = ko.Model.extend({
name: 'friend',
observables: {
firstName: '',
lastName: '',
id: null
}
});
var model = new Model();
model.update(1382539084406, { firstName: 'Linus' }, function () {
model.findOne(1382539084406, function (data) {
console.log(data);
// { firstName: 'Linus', lastName: 'Cadenhead', id: 1382539084406 }
});
});
Instead of using update and insert, you can use save. If there is an id attribute, save will do an update, otherwise, it will do an insert.
var Model = ko.Model.extend({
name: 'friend',
observables: {
firstName: '',
lastName: '',
id: null
}
});
var model = new Model();
model.save({ firstName: 'Jonathan', lastName: 'Creamer', id: 1 }, function () {
console.log('Saved him!');
});
Removes a record from the database.
var Model = ko.Model.extend({
name: 'friend',
observables: {
firstName: '',
lastName: '',
id: null
}
});
var model = new Model();
model.remove(1382539084406, function () {
console.log('1382539084406 was removed');
});
Returns a single viewModel with the matching id.
var Model = ko.Model.extend({
name: 'friend',
observables: {
firstName: '',
lastName: '',
id: null
}
});
var model = new Model();
model.save({ firstName: 'Jonathan', lastName: 'Creamer', id: 1 }, function () {
models.findOne(1, function (data) {
console.log(data);
// { firstName: 'Jonathan', lastName: 'Creamer', id: 1 }
});
});
Search the data for any matches. All matches are returned in the promise.
var Model = ko.Model.extend({
name: 'friends',
observables: {
firstName: '',
lastName: '',
id: null
}
});
var model = new Model();
model.save({ firstName: 'Jonathan', lastName: 'Creamer', id: 1 });
model.save({ firstName: 'Tyson', lastName: 'Cadenhead', id: 2 });
model.save({ firstName: 'Linus', lastName: 'Cadenhead', id: 3 });
model.find({
lastName: 'Cadenhead'
}, function (data) {
console.log(data);
// [{ firstName: 'Tyson', lastName: 'Cadenhead', id: 2 }, { firstName: 'Linus', lastName: 'Cadenhead', id: 3 }]
});
There are a ton of validations you can do on the model before it is allowed to save to a backend service.
The model can publish and subscribe to messages.