-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone-templar.js
178 lines (150 loc) · 6.65 KB
/
backbone-templar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
(function(factory){
if (typeof define === 'function' && define.amd) {
define(['underscore', 'backbone', 'backbone-localStorage'], factory);
} else {
factory(_, Backbone);
}
})(function (_, Backbone){
var config = {
url : 'public/templates/', // template path
ext : '.hbs', // template file extension
version : '0.0.1', // version number
cache : true, // turn local storage caching on/off
storage : new Backbone.LocalStorage('Templar') // default local storage plugin
};
Backbone.Templar = function( templatePaths, options ) {
// if no options are passed
var options = ( typeof options === 'undefined' ) ? {} : options,
templateExtension = ( options.ext ) ? options.ext : config.ext,
baseUrlPath = ( options.url ) ? options.url : config.url,
version = ( options.version ) ? options.version : config.version,
cache = ( typeof options.cache !== 'undefined' ) ? options.cache : config.cache,
storage = ( options.storage ) ? options.storage : config.storage;
// -- public methods
this.render = function( options ) {
// set up defaults
if ( !options.path ) throw 'A template path is required.';
options.el = ( options.el ) ? options.el : document.body;
TemplarCollection._render( options );
}
this.add = function() {
// set up defaults
if ( !options.path ) throw 'A template path is required.';
TemplarCollection._add( options );
}
var TemplarCollection = new(Backbone.Collection.extend({
initialize : function() {
this._loadTemplates();
},
templates : templatePaths,
model : Backbone.Model.extend({
// Note: template file extension is an attribute set in config defaulting to .hbs
defaults : function() {
return {
path : null,
content : null, // the stringified content of the template
version : version
};
},
url : function() {
var path = ( ( this.get('path') && ( this.get('path') !== 'undefined' ) )
? this.get('path')
: '' );
return baseUrlPath + path + templateExtension;
},
parse : function(response) {
return {
content : response
};
}
}),
localStorage : storage,
// -- private methods
_loadTemplates : function() {
var self = this;
var templates = this.localStorage.findAll();
// check for version change
if ( templates.length > 0 && templates[0].version !== version ) {
cache = false; // destroy cached versions and reload new version of templates
}
if ( cache ) {
for (var i = 0; i < templates.length; i++) {
this.add(templates[i]);
}
} else {
// if our cache setting is turned off, never remember our templates
// basically a dev mode flag for debugging templates
_.each(templates, function(model) {
self.localStorage.destroy(model);
});
}
if ( this.templates ) {
for (var i = 0; i < this.templates.length; i++) {
this._add({
path : this.templates[i]
});
}
}
},
_createModel : function(pathString) {
var templateModel = new this.model({
path : pathString
});
return templateModel;
},
_add : function(options, cb) {
var templateCheck = this.where({ path : options.path });
//if there is already an existing stored template
if( templateCheck.length == 0 ) {
// grab the template from the file system
templateModel = this._createModel(options.path);
$.ajax({
url : baseUrlPath + options.path + templateExtension,
async : false,
success : function(response) {
templateModel.set({
'content' : response,
'path' : options.path
});
}
});
// store template model in localStorage
this.add( templateModel );
this.create( templateModel.toJSON() );
if( typeof cb === 'function' ) {
cb();
}
}
},
_compile : function(content, el, append, data, cb) {
var append = ( append ) ? append : false,
compile = Handlebars.compile(content);
if (append) {
el.append(compile(data));
} else {
el.html(compile(data));
}
// run a callback if provided through options.cb
if( typeof cb === 'function' ) {
cb();
}
},
_render : function( options ) {
var self = this;
var templateCheck = this.where({
'path' : options.path,
'version' : version
});
//if there is already an existing stored template
if( templateCheck.length > 0 ) {
this._compile(templateCheck[0].get('content'), options.el, options.append, options.data, options.cb);
} else {
this._add(options, _.bind(function() {
self._compile( templateModel.get('content'), options.el, options.append, options.data, options.cb )
}, this));
}
}
}));
}
return Backbone.Templar;
});