Backbone-inspired PrototypeJS Model extension - Enables Prototype JS users to fetch / store Models from / to a backend using AJAX / REST
This addition to the PrototypeJS Library enables users to define Entity Models and store / fetch (make persistent) them to a backend. Also known as the "M" from MVC.
This library is heavily inspired (but not copied) by the famous Backbone library. Most of the methods use the same name. It exists mainly for an old project with no possibility to switch to a Model-enabled framework like backbone.
with Bower:
bower install https://github.com/bylexus/protobone.git
manually using GIT:
git clone https://github.com/bylexus/protobone.git
<!-- require prototype and the Protobone addition: -->
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js"></script>
<script src="https://github.com/bylexus/protobone/raw/master/protobone/dist/protobone.min.js"></script>
<!-- Usage example -->
<script>
// Create your own Model class:
var Person = Class.create(Protobone.Model,{
urlRoot: '/entity/Person'
});
// Use an instance of the model:
var alex = new Person({
name: 'Schenkel',
firstname: 'Alex'
});
// add an update listener:
alex.on('updated', function(rec,newVals, oldVals) {
console.log("new values of "+rec.get('name'),newVals);
});
alex.set('age','too old');
// Make it persistent:
alex.save({onSuccess: function(res,model){
console.log(model.getId());
}});
// Load an entity:
alex = new Person({id: 4});
alex.fetch({onSuccess: function() {
console.log('done!');
}});
</script>
<!-- load prototype and requirejs: -->
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js"></script>
<script src="http://requirejs.org/docs/release/2.1.16/minified/require.js"></script>
<script>
// load the Protobone class as AMD module:
require.config({
paths: {
'Protobone': 'protobone/dist/protobone.min'
}
});
require(['Protobone'], function(Protobone) {
// Create your own Model class:
var Person = Class.create(Protobone.Model,{
urlRoot: '/entity/Person'
});
// Use an instance of the model:
var alex = new Person({
name: 'Schenkel',
firstname: 'Alex'
});
// add an update listener:
alex.on('updated', function(rec,newVals, oldVals) {
console.log("new values of "+rec.get('name'),newVals);
});
alex.set('age','too old');
// Make it persistent:
alex.save({onSuccess: function(res,model){
console.log(model.getId());
}});
// Load an entity:
alex = new Person({id: 4});
alex.fetch({onSuccess: function() {
console.log('done!');
}});
});
</script>
Please find the API docs online:
git clone https://github.com/bylexus/protobone.git
npm install
run tests:
grunt test
build debug and minified version:
grunt uglify
build docs:
grunt doc
There is still a lot to do. This addition is not yet finished. This is what still need to be done:
Licensed under the MIT license. Copyright 2015 by Alexander Schenkel alex@alexi.ch