xonecas.io
1361
Xc

xonecas.com / commit 1918f5e

created new branch to play with experimental ideas

sean2011-09-13 1918f5e840b74a70dcad7099e7d2b0f789ff357f parent 6cbc093

11 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

index.html+26−58
@@ -8,75 +8,43 @@
88
99 <meta name="viewport" content="width=device-width, initial-scale=1.0">
1010
11- <link rel="stylesheet" href="css/style.css?v=0.0.2">
1211
13- <script src="libs/modernizr.js?v=0.0.2"></script>
12+
13+ <link rel="stylesheet/less" type="text/css" href="less/style.less?v=0.0.3">
14+ <script src="vendor/less.js?v=0.0.3"></script>
15+ <script src="vendor/modernizr.js?v=0.0.3"></script>
1416 </head>
1517 <body>
16- <header>
17- <em class=relTime>
18- Built with:
19- <a href="//xonecas.tumblr.com/" title="Tumblr">Tumblr</a>
20- <a href="//twitter.com/xonecas" title="Tumblr">Twitter</a>
21- <a href="//coderwall.com/xonecas" title="Tumblr">Coderwall</a>
22- </em>
23- <h1><img id="brand" src="imgs/me.png" alt="me!">Sean Caetano Martin</h1>
24- </header>
25-
26- <section id="content">
27- <ul id="tumblr">
28- <li><img class="loader" src="imgs/ajax-loader.gif" alt="Loading..."></li>
29- </ul>
30- </section>
31-
32- <section id="coderwall" class="clearfix">
33- <img class="loader" src="imgs/ajax-loader.gif" alt="Loading...">
34- </section>
35-
36- <section id="latestTweet">
37- <img class="loader" src="imgs/ajax-loader.gif" alt="Loading...">
38- </section>
39-
4018
4119 <!--
4220 Templates go in this magic div that no one sees
4321 -->
44- <div class="hidden">
45- <script id="_latestTweet" type="text/template">
46- <div class="">
47- <strong>@<a href="//twitter.com/xonecas">xonecas</a></strong>
48- <span><%= text %></span>
49- <em><%= relTime %></em>
50- </div>
51- </script>
52-
53-
54- <script id="_tumblr" type="text/template">
55- <li id="<%= id %>" class="post clearfix">
56- <span class="relTime"><%= relTime %></span>
57- <h2 class="title">
58- <%= title %>
59- </h2>
60- <div class="body">
61- <%= body %>
62- </div>
63- </li>
64- </script>
65-
22+ <script id="post_tmpl" type="text/template">
23+ <h1>{{title}}</h1>
24+ <div class="post_body">
25+ {{{body}}}
26+ </div>
27+ <aside>
28+ <a href="{{post_url}}" title="{{post_url}}">permalink</a>,
29+ on {{date}} ({{timestamp}}).
30+ </aside>
31+ </script>
32+
33+ <script id="error_tmpl" type="text/template">
34+ An error has occured: {{error}}. <small>Leaving in 5 secs</small>
35+ </script>
6636
67- <script id="_coderwall" type="text/template">
68- <div class="badge clearfix">
69- <img class="badgeImage" src="<%= badge %>" alt="<%= name %>">
70- <h3 class="badgeTitle"><%= name %></h3>
71- <p class="badgeText"><%= description %></p>
72- </div>
73- </script>
74- </div>
7537
7638 <!--
7739 Dependencies
7840 -->
79- <script src="libs/all.js?v=0.0.2"></script>
80- <script src="js/script.js?v=0.0.2"></script>
41+ <script src="vendor/json2.js?v=0.0.3"></script>
42+ <script src="vendor/jquery.js?v=0.0.3"></script>
43+ <script src="vendor/handlebars.js?v=0.0.3"></script>
44+ <script src="vendor/underscore.js?v=0.0.3"></script>
45+ <script src="vendor/backbone.js?v=0.0.3"></script>
46+
47+ <!-- app -->
48+ <script src="js/script.js?v=0.0.3"></script>
8149 </body>
8250 </html>
js/script.js+65−196
@@ -1,66 +1,17 @@
11 $(function () {
2-
3- // xonecas namespace
4- // -----------------
5- var x = window.x = {};
6-
7- // Helpers
8- // -------
9- // {{{ 8<
10- function relativeTime(time) {
11- time = _.isNumber(time) ? time * 1000 : time;
12-
13- var then = +new Date(time),
14- now = +new Date(),
15- timestamp = now - then,
16- days = 0, hours = 0;
17-
18- days = Math.floor(timestamp / (1000 * 60 * 60 * 24));
19- hours = Math.floor(timestamp / (1000 * 60 * 60));
20-
21- if (days === 1)
22- timestamp = "yesterday";
23- else if (days > 31)
24- timestamp = "more than a month ago";
25- else if (days > 1)
26- timestamp = days + " days ago";
27- else if (hours === 1)
28- timestamp = "an hour ago";
29- else if (hours > 1)
30- timestamp = hours + " hours ago";
31- else
32- timestamp = "a few moments ago";
33-
34- return timestamp +".";
35- }
36-
37- function linkit (text) {
38- text = text.replace(
39- /(ftp|http|https|file):\/\/[\S]+(\b|$)/gim,
40- '<a href="$&" target="_new">$&</a>');
41- text = text.replace(/@\w+\s/gi, function (match) {
42- match = match.substring(0, match.length -1);
43- return '<a href="//twitter.com/'+match.substring(1)+'">'+match+'</a> ';
44- });
45-
46- return text;
47- }
48- // }}}
49-
50- // Tumblr client
51- // -------------
52- // {{{ 8<
53- x.Blog = Backbone.Collection.extend({
2+ Tumblr = Backbone.Collection.extend({
543 url: "http://api.tumblr.com/v2/blog/xonecas.tumblr.com/posts/text",
554 page: 0,
56- count: 5,
57-
58- parse: function (res, xhr) {
5+ count: 6,
6+ idx: 0,
597
60- _.each(res.response.posts, function (post, idx) {
61- post.relTime = relativeTime(post.timestamp);
62- });
8+ initialize: function () {
9+ _.bindAll(this, "setCurrent");
10+ this.bind('reset', this.setCurrent);
11+ },
6312
13+ parse: function (res, xhr) {
14+ this.total = res.response.total_posts;
6415 return res.response.posts;
6516 },
6617
@@ -77,173 +28,91 @@ $(function () {
7728 });
7829
7930 return $.ajax(params);
80- }
81- });
82-
83- x.BlogView = Backbone.View.extend({
84- collection: new x.Blog(),
85- el: $('#tumblr'),
86- template: $('#_tumblr').html(),
87-
88- initialize: function () {
89- this.collection.fetch();
90-
91- _.bindAll(this, 'render');
92- this.collection.bind('reset', this.render);
9331 },
9432
95- render: function () {
96- var that = this;
97-
98- this.el.empty();
99- this.collection.forEach(function (el) {
100- that.el.append(
101- _.template(that.template, el.toJSON())
102- );
103- });
104- this.visible = 0;
105- $("#"+this.collection.at(0).get('id'))
106- .addClass('selectedPost');
107- }
108- });
109- // }}}
110-
111- // Twitter client
112- // --------------
113- // {{{ 8<
114- x.Tweets = Backbone.Collection.extend({
115- url: "http://api.twitter.com/1/statuses/user_timeline.json",
33+ setCurrent: function () {
34+ this.current = this.at(this.idx);
35+ },
11636
117- parse: function (res, xhr) {
118- _.each(res, function (tweet, idx) {
119- tweet.relTime = relativeTime(tweet.created_at);
120- tweet.text = linkit(tweet.text);
121- });
37+ next: function () {
38+ this.idx++;
39+ var model = this.at(this.idx);
12240
123- return res;
41+ if (model) {
42+ this.current = model;
43+ } else {
44+ this.page++;
45+ this.idx = 0;
46+ this.fetch();
47+ }
12448 },
12549
126- sync: function (model, method, options) {
127- var params = _.extend(options, {
128- "url": this.url,
129- "data": {
130- "screen_name": "xonecas",
131- "count": 10,
132- "trim_user": true,
133- "include_rts": true,
134- "exclude_replies": true
135- },
136- "dataType": "jsonp"
137- });
50+ previous: function () {
51+ this.idx--;
52+ var model = this.at(this.idx);
53+
54+ if (model) {
55+ this.current = model;
56+ } else {
57+ if (this.page > 0) {
58+ this.page--;
59+ this.idx = this.count -1;
60+ this.fetch();
61+ } else {
62+ this.trigger('error', 'At most recent post');
63+ }
64+ }
13865
139- return $.ajax(params);
14066 }
141-
14267 });
14368
144- x.TweetsView = Backbone.View.extend({
145- collection: new x.Tweets(),
146- el: $('#latestTweet'),
147- template: $('#_latestTweet').html(),
69+ ErrorReport = Backbone.View.extend({
70+ id: 'error',
71+ className: 'container',
72+ template: Handlebars.compile($('#error_tmpl').html()),
14873
14974 initialize: function () {
150- _.bindAll(this, 'render', 'loop');
151- this.collection.bind('reset', this.loop);
152- this.collection.fetch();
153- },
154-
155- render: function () {
156- var random = Math.floor(Math.random() * this.collection.length),
157- tweet = this.collection.at(random);
158-
159- this.el.html(_.template(this.template, tweet.toJSON()));
160- },
161-
162- loop: function () {
75+ _.bindAll(this, "remove");
16376 this.render();
164-
165- setTimeout(this.loop, 60 * 1000);
166- }
167- });
168- // }}}
169-
170- // Coderwall client
171- // ----------------
172- // {{{ 8<
173- x.Badges = Backbone.Collection.extend({
174- url: "http://coderwall.com/xonecas.json",
175-
176- parse: function (res, xhr) {
177- return res.data.badges;
17877 },
179-
180- sync: function (model, method, options) {
181- var params = _.extend(options, {
182- "url": this.url,
183- "dataType": "jsonp"
184- });
18578
186- return $.ajax(params);
79+ render: function () {
80+ $('#main').append(this.template(this.error));
81+ setTimeout(this.remove, 5000);
82+ return this;
18783 }
18884 });
18985
190- x.BadgesView = Backbone.View.extend({
191- collection: new x.Badges(),
192- el: $("#coderwall"),
193- template: $('#_coderwall').html(),
86+ HomePage = Backbone.View.extend({
87+ id: 'main',
88+ className: 'container',
89+ template: Handlebars.compile($('#post_tmpl').html()),
90+ collection: new Tumblr(),
19491
19592 initialize: function () {
196- _.bindAll(this, 'render');
93+ _.bindAll(this, "render", "report");
19794 this.collection.bind('reset', this.render);
95+ this.collection.bind('error', this.report);
19896 this.collection.fetch();
97+
98+ $(this.el).appendTo('body');
19999 },
200100
201101 render: function () {
202- var that = this;
203-
204- this.el.empty();
205- this.collection.each(function (badge) {
206- that.el.append(_.template(that.template, badge.toJSON()));
207- });
208- }
209- });
210- // }}}
211-
212- x.IndexView = Backbone.View.extend({
213- el: $(document),
214-
215- initialize: function () {
216- this.blog = new x.BlogView();
217- this.tweet = new x.TweetsView();
218- this.badges = new x.BadgesView();
219- },
220-
221- events: {
222- 'click' : "keypressRouter"
102+ var model = this.collection.current;
103+ $(this.el)
104+ .css('display', 'none')
105+ .html(this.template(model.toJSON()))
106+ .fadeIn(1000);
107+
223108 },
224109
225- keypressRouter: function (ev) {
226- switch (ev.which) {
227- case 106:
228- case 107:
229- this.jumpTo(ev);
230- break;
231- case 47:
232- ev.preventDefault();
233- break;
234- default:
235- if (console && console.log) {
236- console.log(ev.which);
237- }
238- }
239-
110+ report: function (error) {
111+ this.errors = this.errors || [];
112+ this.errors.push(new ErrorReport().render());
240113 }
241114 });
242115
243116 // main
244- // ----
245- x.index = new x.IndexView();
246-
247- return x;
248-
117+ window.app = new HomePage();
249118 });
less/mixins.less+27−0
@@ -0,0 +1,27 @@
1+// From https://github.com/twitter/bootstrap/blob/master/lib/preboot.less
2+
3+// ERIC MEYER RESET
4+// --------------------------------------------------
5+
6+html, body { margin: 0; padding: 0; }
7+h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, cite, code, del, dfn, em, img, q, s, samp, small, strike, strong, sub, sup, tt, var, dd, dl, dt, li, ol, ul, fieldset, form, label, legend, button, table, caption, tbody, tfoot, thead, tr, th, td { margin: 0; padding: 0; border: 0; font-weight: normal; font-style: normal; font-size: 100%; line-height: 1; font-family: inherit; }
8+table { border-collapse: collapse; border-spacing: 0; }
9+ol, ul { list-style: none; }
10+q:before, q:after, blockquote:before, blockquote:after { content: ""; }
11+
12+// clearfix
13+.clearfix {
14+ zoom: 1;
15+ &:before, &:after {
16+ display: table;
17+ content: "";
18+ }
19+ &:after {
20+ clear: both;
21+ }
22+}
23+
24+.sans {
25+ font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
26+}
27+
less/style.less+42−0
@@ -0,0 +1,42 @@
1+@import "mixins";
2+
3+// config
4+@siteWidth: 720px;
5+@siteMargin: 20px 20px 20px 40px;
6+@lineHeight: 24px;
7+@fontSize: 16px;
8+
9+// colors from http://www.colourlovers.com/palette/444487/Curiosity_Killed
10+@bg: #2E2633; //eveningcloud
11+@fg: #EFFFCD; //vichy
12+@visited: #555152; //dirt
13+@link: #DCE9BE; //hex
14+@highlight: #99173C; //helio
15+
16+
17+body {
18+ width: @siteWidth;
19+ margin: @siteMargin;
20+ background-color: @bg;
21+ color: @fg;
22+ font-size: @fontSize;
23+ line-height: @lineHeight;
24+ .sans();
25+ .clearfix();
26+}
27+
28+a {
29+ color: @link;
30+ text-decoration: none;
31+ font-size: @fontSize;
32+ line-height: @lineHeight;
33+ &:visited, &:hover {
34+ color: @visited;
35+ text-decoration: underline;
36+ }
37+}
38+
39+small {
40+ font-size: @fontSize -3;
41+ line-height: @lineHeight -3;
42+}
vendor/backbone.js+1158−0
@@ -0,0 +1,1158 @@
1+// Backbone.js 0.5.3
2+// (c) 2010 Jeremy Ashkenas, DocumentCloud Inc.
3+// Backbone may be freely distributed under the MIT license.
4+// For all details and documentation:
5+// http://documentcloud.github.com/backbone
6+
7+(function(){
8+
9+ // Initial Setup
10+ // -------------
11+
12+ // Save a reference to the global object.
13+ var root = this;
14+
15+ // Save the previous value of the `Backbone` variable.
16+ var previousBackbone = root.Backbone;
17+
18+ // The top-level namespace. All public Backbone classes and modules will
19+ // be attached to this. Exported for both CommonJS and the browser.
20+ var Backbone;
21+ if (typeof exports !== 'undefined') {
22+ Backbone = exports;
23+ } else {
24+ Backbone = root.Backbone = {};
25+ }
26+
27+ // Current version of the library. Keep in sync with `package.json`.
28+ Backbone.VERSION = '0.5.3';
29+
30+ // Require Underscore, if we're on the server, and it's not already present.
31+ var _ = root._;
32+ if (!_ && (typeof require !== 'undefined')) _ = require('underscore')._;
33+
34+ // For Backbone's purposes, jQuery or Zepto owns the `$` variable.
35+ var $ = root.jQuery || root.Zepto;
36+
37+ // Runs Backbone.js in *noConflict* mode, returning the `Backbone` variable
38+ // to its previous owner. Returns a reference to this Backbone object.
39+ Backbone.noConflict = function() {
40+ root.Backbone = previousBackbone;
41+ return this;
42+ };
43+
44+ // Turn on `emulateHTTP` to support legacy HTTP servers. Setting this option will
45+ // fake `"PUT"` and `"DELETE"` requests via the `_method` parameter and set a
46+ // `X-Http-Method-Override` header.
47+ Backbone.emulateHTTP = false;
48+
49+ // Turn on `emulateJSON` to support legacy servers that can't deal with direct
50+ // `application/json` requests ... will encode the body as
51+ // `application/x-www-form-urlencoded` instead and will send the model in a
52+ // form param named `model`.
53+ Backbone.emulateJSON = false;
54+
55+ // Backbone.Events
56+ // -----------------
57+
58+ // A module that can be mixed in to *any object* in order to provide it with
59+ // custom events. You may `bind` or `unbind` a callback function to an event;
60+ // `trigger`-ing an event fires all callbacks in succession.
61+ //
62+ // var object = {};
63+ // _.extend(object, Backbone.Events);
64+ // object.bind('expand', function(){ alert('expanded'); });
65+ // object.trigger('expand');
66+ //
67+ Backbone.Events = {
68+
69+ // Bind an event, specified by a string name, `ev`, to a `callback` function.
70+ // Passing `"all"` will bind the callback to all events fired.
71+ bind : function(ev, callback, context) {
72+ var calls = this._callbacks || (this._callbacks = {});
73+ var list = calls[ev] || (calls[ev] = []);
74+ list.push([callback, context]);
75+ return this;
76+ },
77+
78+ // Remove one or many callbacks. If `callback` is null, removes all
79+ // callbacks for the event. If `ev` is null, removes all bound callbacks
80+ // for all events.
81+ unbind : function(ev, callback) {
82+ var calls;
83+ if (!ev) {
84+ this._callbacks = {};
85+ } else if (calls = this._callbacks) {
86+ if (!callback) {
87+ calls[ev] = [];
88+ } else {
89+ var list = calls[ev];
90+ if (!list) return this;
91+ for (var i = 0, l = list.length; i < l; i++) {
92+ if (list[i] && callback === list[i][0]) {
93+ list[i] = null;
94+ break;
95+ }
96+ }
97+ }
98+ }
99+ return this;
100+ },
101+
102+ // Trigger an event, firing all bound callbacks. Callbacks are passed the
103+ // same arguments as `trigger` is, apart from the event name.
104+ // Listening for `"all"` passes the true event name as the first argument.
105+ trigger : function(eventName) {
106+ var list, calls, ev, callback, args;
107+ var both = 2;
108+ if (!(calls = this._callbacks)) return this;
109+ while (both--) {
110+ ev = both ? eventName : 'all';
111+ if (list = calls[ev]) {
112+ for (var i = 0, l = list.length; i < l; i++) {
113+ if (!(callback = list[i])) {
114+ list.splice(i, 1); i--; l--;
115+ } else {
116+ args = both ? Array.prototype.slice.call(arguments, 1) : arguments;
117+ callback[0].apply(callback[1] || this, args);
118+ }
119+ }
120+ }
121+ }
122+ return this;
123+ }
124+
125+ };
126+
127+ // Backbone.Model
128+ // --------------
129+
130+ // Create a new model, with defined attributes. A client id (`cid`)
131+ // is automatically generated and assigned for you.
132+ Backbone.Model = function(attributes, options) {
133+ var defaults;
134+ attributes || (attributes = {});
135+ if (defaults = this.defaults) {
136+ if (_.isFunction(defaults)) defaults = defaults.call(this);
137+ attributes = _.extend({}, defaults, attributes);
138+ }
139+ this.attributes = {};
140+ this._escapedAttributes = {};
141+ this.cid = _.uniqueId('c');
142+ this.set(attributes, {silent : true});
143+ this._changed = false;
144+ this._previousAttributes = _.clone(this.attributes);
145+ if (options && options.collection) this.collection = options.collection;
146+ this.initialize(attributes, options);
147+ };
148+
149+ // Attach all inheritable methods to the Model prototype.
150+ _.extend(Backbone.Model.prototype, Backbone.Events, {
151+
152+ // A snapshot of the model's previous attributes, taken immediately
153+ // after the last `"change"` event was fired.
154+ _previousAttributes : null,
155+
156+ // Has the item been changed since the last `"change"` event?
157+ _changed : false,
158+
159+ // The default name for the JSON `id` attribute is `"id"`. MongoDB and
160+ // CouchDB users may want to set this to `"_id"`.
161+ idAttribute : 'id',
162+
163+ // Initialize is an empty function by default. Override it with your own
164+ // initialization logic.
165+ initialize : function(){},
166+
167+ // Return a copy of the model's `attributes` object.
168+ toJSON : function() {
169+ return _.clone(this.attributes);
170+ },
171+
172+ // Get the value of an attribute.
173+ get : function(attr) {
174+ return this.attributes[attr];
175+ },
176+
177+ // Get the HTML-escaped value of an attribute.
178+ escape : function(attr) {
179+ var html;
180+ if (html = this._escapedAttributes[attr]) return html;
181+ var val = this.attributes[attr];
182+ return this._escapedAttributes[attr] = escapeHTML(val == null ? '' : '' + val);
183+ },
184+
185+ // Returns `true` if the attribute contains a value that is not null
186+ // or undefined.
187+ has : function(attr) {
188+ return this.attributes[attr] != null;
189+ },
190+
191+ // Set a hash of model attributes on the object, firing `"change"` unless you
192+ // choose to silence it.
193+ set : function(attrs, options) {
194+
195+ // Extract attributes and options.
196+ options || (options = {});
197+ if (!attrs) return this;
198+ if (attrs.attributes) attrs = attrs.attributes;
199+ var now = this.attributes, escaped = this._escapedAttributes;
200+
201+ // Run validation.
202+ if (!options.silent && this.validate && !this._performValidation(attrs, options)) return false;
203+
204+ // Check for changes of `id`.
205+ if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];
206+
207+ // We're about to start triggering change events.
208+ var alreadyChanging = this._changing;
209+ this._changing = true;
210+
211+ // Update attributes.
212+ for (var attr in attrs) {
213+ var val = attrs[attr];
214+ if (!_.isEqual(now[attr], val)) {
215+ now[attr] = val;
216+ delete escaped[attr];
217+ this._changed = true;
218+ if (!options.silent) this.trigger('change:' + attr, this, val, options);
219+ }
220+ }
221+
222+ // Fire the `"change"` event, if the model has been changed.
223+ if (!alreadyChanging && !options.silent && this._changed) this.change(options);
224+ this._changing = false;
225+ return this;
226+ },
227+
228+ // Remove an attribute from the model, firing `"change"` unless you choose
229+ // to silence it. `unset` is a noop if the attribute doesn't exist.
230+ unset : function(attr, options) {
231+ if (!(attr in this.attributes)) return this;
232+ options || (options = {});
233+ var value = this.attributes[attr];
234+
235+ // Run validation.
236+ var validObj = {};
237+ validObj[attr] = void 0;
238+ if (!options.silent && this.validate && !this._performValidation(validObj, options)) return false;
239+
240+ // Remove the attribute.
241+ delete this.attributes[attr];
242+ delete this._escapedAttributes[attr];
243+ if (attr == this.idAttribute) delete this.id;
244+ this._changed = true;
245+ if (!options.silent) {
246+ this.trigger('change:' + attr, this, void 0, options);
247+ this.change(options);
248+ }
249+ return this;
250+ },
251+
252+ // Clear all attributes on the model, firing `"change"` unless you choose
253+ // to silence it.
254+ clear : function(options) {
255+ options || (options = {});
256+ var attr;
257+ var old = this.attributes;
258+
259+ // Run validation.
260+ var validObj = {};
261+ for (attr in old) validObj[attr] = void 0;
262+ if (!options.silent && this.validate && !this._performValidation(validObj, options)) return false;
263+
264+ this.attributes = {};
265+ this._escapedAttributes = {};
266+ this._changed = true;
267+ if (!options.silent) {
268+ for (attr in old) {
269+ this.trigger('change:' + attr, this, void 0, options);
270+ }
271+ this.change(options);
272+ }
273+ return this;
274+ },
275+
276+ // Fetch the model from the server. If the server's representation of the
277+ // model differs from its current attributes, they will be overriden,
278+ // triggering a `"change"` event.
279+ fetch : function(options) {
280+ options || (options = {});
281+ var model = this;
282+ var success = options.success;
283+ options.success = function(resp, status, xhr) {
284+ if (!model.set(model.parse(resp, xhr), options)) return false;
285+ if (success) success(model, resp);
286+ };
287+ options.error = wrapError(options.error, model, options);
288+ return (this.sync || Backbone.sync).call(this, 'read', this, options);
289+ },
290+
291+ // Set a hash of model attributes, and sync the model to the server.
292+ // If the server returns an attributes hash that differs, the model's
293+ // state will be `set` again.
294+ save : function(attrs, options) {
295+ options || (options = {});
296+ if (attrs && !this.set(attrs, options)) return false;
297+ var model = this;
298+ var success = options.success;
299+ options.success = function(resp, status, xhr) {
300+ if (!model.set(model.parse(resp, xhr), options)) return false;
301+ if (success) success(model, resp, xhr);
302+ };
303+ options.error = wrapError(options.error, model, options);
304+ var method = this.isNew() ? 'create' : 'update';
305+ return (this.sync || Backbone.sync).call(this, method, this, options);
306+ },
307+
308+ // Destroy this model on the server if it was already persisted. Upon success, the model is removed
309+ // from its collection, if it has one.
310+ destroy : function(options) {
311+ options || (options = {});
312+ if (this.isNew()) return this.trigger('destroy', this, this.collection, options);
313+ var model = this;
314+ var success = options.success;
315+ options.success = function(resp) {
316+ model.trigger('destroy', model, model.collection, options);
317+ if (success) success(model, resp);
318+ };
319+ options.error = wrapError(options.error, model, options);
320+ return (this.sync || Backbone.sync).call(this, 'delete', this, options);
321+ },
322+
323+ // Default URL for the model's representation on the server -- if you're
324+ // using Backbone's restful methods, override this to change the endpoint
325+ // that will be called.
326+ url : function() {
327+ var base = getUrl(this.collection) || this.urlRoot || urlError();
328+ if (this.isNew()) return base;
329+ return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.id);
330+ },
331+
332+ // **parse** converts a response into the hash of attributes to be `set` on
333+ // the model. The default implementation is just to pass the response along.
334+ parse : function(resp, xhr) {
335+ return resp;
336+ },
337+
338+ // Create a new model with identical attributes to this one.
339+ clone : function() {
340+ return new this.constructor(this);
341+ },
342+
343+ // A model is new if it has never been saved to the server, and lacks an id.
344+ isNew : function() {
345+ return this.id == null;
346+ },
347+
348+ // Call this method to manually fire a `change` event for this model.
349+ // Calling this will cause all objects observing the model to update.
350+ change : function(options) {
351+ this.trigger('change', this, options);
352+ this._previousAttributes = _.clone(this.attributes);
353+ this._changed = false;
354+ },
355+
356+ // Determine if the model has changed since the last `"change"` event.
357+ // If you specify an attribute name, determine if that attribute has changed.
358+ hasChanged : function(attr) {
359+ if (attr) return this._previousAttributes[attr] != this.attributes[attr];
360+ return this._changed;
361+ },
362+
363+ // Return an object containing all the attributes that have changed, or false
364+ // if there are no changed attributes. Useful for determining what parts of a
365+ // view need to be updated and/or what attributes need to be persisted to
366+ // the server.
367+ changedAttributes : function(now) {
368+ now || (now = this.attributes);
369+ var old = this._previousAttributes;
370+ var changed = false;
371+ for (var attr in now) {
372+ if (!_.isEqual(old[attr], now[attr])) {
373+ changed = changed || {};
374+ changed[attr] = now[attr];
375+ }
376+ }
377+ return changed;
378+ },
379+
380+ // Get the previous value of an attribute, recorded at the time the last
381+ // `"change"` event was fired.
382+ previous : function(attr) {
383+ if (!attr || !this._previousAttributes) return null;
384+ return this._previousAttributes[attr];
385+ },
386+
387+ // Get all of the attributes of the model at the time of the previous
388+ // `"change"` event.
389+ previousAttributes : function() {
390+ return _.clone(this._previousAttributes);
391+ },
392+
393+ // Run validation against a set of incoming attributes, returning `true`
394+ // if all is well. If a specific `error` callback has been passed,
395+ // call that instead of firing the general `"error"` event.
396+ _performValidation : function(attrs, options) {
397+ var error = this.validate(attrs);
398+ if (error) {
399+ if (options.error) {
400+ options.error(this, error, options);
401+ } else {
402+ this.trigger('error', this, error, options);
403+ }
404+ return false;
405+ }
406+ return true;
407+ }
408+
409+ });
410+
411+ // Backbone.Collection
412+ // -------------------
413+
414+ // Provides a standard collection class for our sets of models, ordered
415+ // or unordered. If a `comparator` is specified, the Collection will maintain
416+ // its models in sort order, as they're added and removed.
417+ Backbone.Collection = function(models, options) {
418+ options || (options = {});
419+ if (options.comparator) this.comparator = options.comparator;
420+ _.bindAll(this, '_onModelEvent', '_removeReference');
421+ this._reset();
422+ if (models) this.reset(models, {silent: true});
423+ this.initialize.apply(this, arguments);
424+ };
425+
426+ // Define the Collection's inheritable methods.
427+ _.extend(Backbone.Collection.prototype, Backbone.Events, {
428+
429+ // The default model for a collection is just a **Backbone.Model**.
430+ // This should be overridden in most cases.
431+ model : Backbone.Model,
432+
433+ // Initialize is an empty function by default. Override it with your own
434+ // initialization logic.
435+ initialize : function(){},
436+
437+ // The JSON representation of a Collection is an array of the
438+ // models' attributes.
439+ toJSON : function() {
440+ return this.map(function(model){ return model.toJSON(); });
441+ },
442+
443+ // Add a model, or list of models to the set. Pass **silent** to avoid
444+ // firing the `added` event for every new model.
445+ add : function(models, options) {
446+ if (_.isArray(models)) {
447+ for (var i = 0, l = models.length; i < l; i++) {
448+ this._add(models[i], options);
449+ }
450+ } else {
451+ this._add(models, options);
452+ }
453+ return this;
454+ },
455+
456+ // Remove a model, or a list of models from the set. Pass silent to avoid
457+ // firing the `removed` event for every model removed.
458+ remove : function(models, options) {
459+ if (_.isArray(models)) {
460+ for (var i = 0, l = models.length; i < l; i++) {
461+ this._remove(models[i], options);
462+ }
463+ } else {
464+ this._remove(models, options);
465+ }
466+ return this;
467+ },
468+
469+ // Get a model from the set by id.
470+ get : function(id) {
471+ if (id == null) return null;
472+ return this._byId[id.id != null ? id.id : id];
473+ },
474+
475+ // Get a model from the set by client id.
476+ getByCid : function(cid) {
477+ return cid && this._byCid[cid.cid || cid];
478+ },
479+
480+ // Get the model at the given index.
481+ at: function(index) {
482+ return this.models[index];
483+ },
484+
485+ // Force the collection to re-sort itself. You don't need to call this under normal
486+ // circumstances, as the set will maintain sort order as each item is added.
487+ sort : function(options) {
488+ options || (options = {});
489+ if (!this.comparator) throw new Error('Cannot sort a set without a comparator');
490+ this.models = this.sortBy(this.comparator);
491+ if (!options.silent) this.trigger('reset', this, options);
492+ return this;
493+ },
494+
495+ // Pluck an attribute from each model in the collection.
496+ pluck : function(attr) {
497+ return _.map(this.models, function(model){ return model.get(attr); });
498+ },
499+
500+ // When you have more items than you want to add or remove individually,
501+ // you can reset the entire set with a new list of models, without firing
502+ // any `added` or `removed` events. Fires `reset` when finished.
503+ reset : function(models, options) {
504+ models || (models = []);
505+ options || (options = {});
506+ this.each(this._removeReference);
507+ this._reset();
508+ this.add(models, {silent: true});
509+ if (!options.silent) this.trigger('reset', this, options);
510+ return this;
511+ },
512+
513+ // Fetch the default set of models for this collection, resetting the
514+ // collection when they arrive. If `add: true` is passed, appends the
515+ // models to the collection instead of resetting.
516+ fetch : function(options) {
517+ options || (options = {});
518+ var collection = this;
519+ var success = options.success;
520+ options.success = function(resp, status, xhr) {
Cut short: this commit's diff is over 1000 lines.
vendor/handlebars.js+1573−0
Cut short: this commit's diff is over 1000 lines.
vendor/jquery.js+9044−0
Cut short: this commit's diff is over 1000 lines.
vendor/json2.js+480−0
Cut short: this commit's diff is over 1000 lines.
vendor/less.js+2769−0
Cut short: this commit's diff is over 1000 lines.
vendor/modernizr.js+1116−0
Cut short: this commit's diff is over 1000 lines.
vendor/underscore.js+839−0
Cut short: this commit's diff is over 1000 lines.