Backbone Basics
Basics library for developing backbone applications.
This is a work in progress
Contents
Basics exports this structures:
Basics.Model
Basics.Collection
Basics.View
This is what this library provides to help:
Nested data structures
To define nested structures:
var Person = Basics.Model.Extend({
defaults: {
name: '',
email: ''
}
})
var Message = Basics.Model.extend({
defaults: {
subject: '',
text: '',
sender: null
},
types: {
sender: Person
}
})
var Messages = Basics.Collection.extend({
model: Message
})
var Inbox = Basics.Model.extend({
defaults: {
owner: null,
messages: null
},
types: {
owner: Person,
messages: Messages
}
})
When creating them (works both passing json to the constructor and calling the
service with .fetch
):
var inbox = new Inbox({
owner: {
name: 'John',
email: 'john@example.com'
},
messages: [{
subject: 'Example 1',
text: 'Text 1',
sender: {
name: 'John',
email: 'john@example.com'
}
}, {
subject: 'Example 2',
text: 'Text 2',
sender: {
name: 'John',
email: 'john@example.com'
}
}]
}, {
parse: true
})
var ownerName = inbox.get('owner').get('name')
, secondMessageSubject = inbox.get('messages').at(1).get('subject')
expect(ownerName).to.eql('John')
expect(secondMessageSubject).to.eql('Example 2')
If you try to create a full empty object to bind it to forms/views, then the
way above wont work. You need the special option unfold: true
like this:
var inbox1 = new Inbox(null, { parse: true })
expect(inbox1.get('owner')).to.be.a('null')
expect(inbox1.get('messages')).to.be.a('null')
var inbox2 = new Inbox(null, { parse: true, unfold: true })
var owner = inbox2.get('owner')
, messages = inbox2.get('messages')
expect(owner).to.be.an.instanceof(Person)
expect(owner.get('name')).to.eql('')
expect(messages).to.be.an.instanceof(Messages)
expect(messages.length).to.eql(0)
Mixin objects
Backbone provides for its types the extend
method to create class
hierarchies.
This is very convenient, but very convenient also is taking advantage of
javascript and using mixins to extract functionality that is independent and
common through classes but does not relate to them in a hierarchical way.
All types from basics have a mixin
method that allows them to mixin with JS
objects that contain variables/functions that encapsulate functionality. The
mixin method is added to the types, not the instances.
You can use it like this:
var asOneTimeModal = {
modalTemplate: _.template(modalTemplateSource),
events: {
'click .modal-header .close' : 'hide',
'click .modal-footer .btn-close': 'hide'
},
show: function() {
var $m = this.$('.modal').modal({})
$m.on('hidden', _.bind(this.remove, this))
},
hide: function() {
this.$('.modal').modal('hide')
this.trigger('close:modal')
}
}
var AddPersonView = Basics.View.extend({
events: {
'click .submitModal': 'addPersonClick'
},
initialize: function(options) {
},
addPersonClick: function(event) {
}
}).mixin(asOneTimeModal)
var newGuy = new Person()
var addPersonForm = new AddPersonView({ model: newGuy })
addPersonForm.show()
The same ideas can be applied to models and collections. For example to create
Pageable collections, lazy loading models, etc. There may be some basic mixins
included in Basics in the future to be mixed with the basics types.
Usage
From a browserify enabled client side module
If you are developing a module or application and using browserify as a module
loader, then just include backbone-basics on the package.json
as a
dependency, and require it normally.
From a server side node.js module
Same as above. Declare the dependency on the package.json
of your module/app
and require it normally.
From a browser side application that does NOT use browserify
You have to grab the file browser/backbone-basics.js
or
browser/backbone-basics-min.js
and include it on your page after the
required dependencies. It will define a global variable on the window object
called Basics
, that you can use then normally.
Development
To get the dependencies do a npm install
Source is on the src
folder.
Tests on the test
folder.
Make actions:
- Compile:
make compile
- Test:
make test
- Tests watcher:
make test-w
TODO
At the root of the source there is a TODO file.