rails-backbone 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -53,7 +53,13 @@ This generator creates a router, views, templates, model and collection to creat
53
53
 
54
54
  ## Example Usage
55
55
 
56
- Say we have just created a new rails 3.1 application called `blog`. Edit your gemfile and add `gem rails-backbone`.
56
+ Created a new rails 3.1 application called `blog`.
57
+
58
+ rails new blog
59
+
60
+ Edit your Gemfile and add
61
+
62
+ gem 'rails-backbone'
57
63
 
58
64
  Install the gem and generate scaffolding.
59
65
 
@@ -73,6 +79,7 @@ Edit your posts index view `app/views/posts/index.html.erb` with the following c
73
79
 
74
80
  <script type="text/javascript">
75
81
  $(function() {
82
+ // Blog is the app name
76
83
  window.router = new Blog.Routers.PostsRouter({posts: <%= @posts.to_json.html_safe -%>});
77
84
  Backbone.history.start();
78
85
  });
@@ -28,7 +28,7 @@ module Backbone
28
28
  protected
29
29
  def application_name
30
30
  if defined?(Rails) && Rails.application
31
- Rails.application.class.name.split('::').first.underscore
31
+ Rails.application.class.name.split('::').first
32
32
  else
33
33
  "application"
34
34
  end
@@ -4,7 +4,7 @@
4
4
  #= require_tree ./views
5
5
  #= require_tree ./routers
6
6
 
7
- window.<%= application_name.capitalize %> =
7
+ window.<%= application_name.camelize %> =
8
8
  Models: {}
9
9
  Collections: {}
10
10
  Routers: {}
@@ -7,25 +7,37 @@ module Backbone
7
7
  end
8
8
 
9
9
  def model_namespace
10
- [application_name.capitalize, "Models", class_name].join(".")
10
+ [application_name.camelize, "Models", class_name].join(".")
11
+ end
12
+
13
+ def singular_model_name
14
+ uncapitalize singular_name.camelize
15
+ end
16
+
17
+ def plural_model_name
18
+ uncapitalize(plural_name.camelize)
11
19
  end
12
20
 
13
21
  def collection_namespace
14
- [application_name.capitalize, "Collections", plural_name.capitalize].join(".")
22
+ [application_name.camelize, "Collections", plural_name.camelize].join(".")
15
23
  end
16
24
 
17
25
  def view_namespace
18
- [application_name.capitalize, "Views", plural_name.capitalize].join(".")
26
+ [application_name.camelize, "Views", plural_name.camelize].join(".")
19
27
  end
20
28
 
21
29
  def router_namespace
22
- [application_name.capitalize, "Routers", plural_name.capitalize].join(".")
30
+ [application_name.camelize, "Routers", plural_name.camelize].join(".")
23
31
  end
24
32
 
25
33
  def jst(action)
26
34
  "backbone/templates/#{plural_name}/#{action}"
27
35
  end
28
36
 
37
+ def uncapitalize(str)
38
+ str[0, 1].downcase + str[1..-1]
39
+ end
40
+
29
41
  end
30
42
  end
31
43
  end
@@ -8,7 +8,7 @@ class <%= router_namespace %>Router extends Backbone.Router
8
8
 
9
9
  <% actions.each do |action| -%>
10
10
  <%= action %>: ->
11
- @view = new <%= "#{view_namespace}.#{action.capitalize}View()" %>
11
+ @view = new <%= "#{view_namespace}.#{action.camelize}View()" %>
12
12
  $("#<%= plural_name %>").html(@view.render().el)
13
13
 
14
14
  <% end -%>
@@ -1,6 +1,6 @@
1
1
  <%= view_namespace %> ||= {}
2
2
 
3
- class <%= view_namespace %>.<%= @action.capitalize %>View extends Backbone.View
3
+ class <%= view_namespace %>.<%= @action.camelize %>View extends Backbone.View
4
4
  template: JST["<%= jst @action %>"]
5
5
 
6
6
  render: ->
@@ -1,7 +1,7 @@
1
1
  class <%= router_namespace %>Router extends Backbone.Router
2
2
  initialize: (options) ->
3
- @<%= plural_name %> = new <%= collection_namespace %>Collection()
4
- @<%= plural_name %>.reset options.<%= plural_name %>
3
+ @<%= plural_model_name %> = new <%= collection_namespace %>Collection()
4
+ @<%= plural_model_name %>.reset options.<%= plural_model_name %>
5
5
 
6
6
  routes:
7
7
  "/new": "new<%= class_name %>"
@@ -6,17 +6,17 @@ class <%= view_namespace %>.IndexView extends Backbone.View
6
6
  initialize: () ->
7
7
  _.bindAll(this, 'addOne', 'addAll', 'render');
8
8
 
9
- @options.<%= plural_name %>.bind('reset', this.addAll);
9
+ @options.<%= plural_model_name %>.bind('reset', this.addAll);
10
10
 
11
11
  addAll: () ->
12
- @options.<%= plural_name %>.each(this.addOne)
12
+ @options.<%= plural_model_name %>.each(this.addOne)
13
13
 
14
- addOne: (<%= singular_name %>) ->
15
- view = new <%= view_namespace %>.<%= singular_name.capitalize %>View({model : <%= singular_name %>})
14
+ addOne: (<%= singular_model_name %>) ->
15
+ view = new <%= view_namespace %>.<%= singular_name.camelize %>View({model : <%= singular_model_name %>})
16
16
  this.$("tbody").append(view.render().el)
17
17
 
18
18
  render: ->
19
- $(this.el).html(this.template(<%= plural_name %>: this.options.<%= plural_name %>.toJSON() ))
19
+ $(this.el).html(this.template(<%= plural_model_name %>: this.options.<%= plural_model_name %>.toJSON() ))
20
20
  @addAll()
21
21
 
22
22
  return this
@@ -1,6 +1,6 @@
1
1
  <%= view_namespace %> ||= {}
2
2
 
3
- class <%= view_namespace %>.<%= singular_name.capitalize %>View extends Backbone.View
3
+ class <%= view_namespace %>.<%= singular_name.camelize %>View extends Backbone.View
4
4
  template: JST["<%= jst singular_name %>"]
5
5
 
6
6
  events:
@@ -1,4 +1,4 @@
1
- // Backbone.js 0.5.1
1
+ // Backbone.js 0.5.2
2
2
  // (c) 2010 Jeremy Ashkenas, DocumentCloud Inc.
3
3
  // Backbone may be freely distributed under the MIT license.
4
4
  // For all details and documentation:
@@ -25,7 +25,7 @@
25
25
  }
26
26
 
27
27
  // Current version of the library. Keep in sync with `package.json`.
28
- Backbone.VERSION = '0.5.1';
28
+ Backbone.VERSION = '0.5.2';
29
29
 
30
30
  // Require Underscore, if we're on the server, and it's not already present.
31
31
  var _ = root._;
@@ -41,7 +41,7 @@
41
41
  return this;
42
42
  };
43
43
 
44
- // Turn on `emulateHTTP` to use support legacy HTTP servers. Setting this option will
44
+ // Turn on `emulateHTTP` to support legacy HTTP servers. Setting this option will
45
45
  // fake `"PUT"` and `"DELETE"` requests via the `_method` parameter and set a
46
46
  // `X-Http-Method-Override` header.
47
47
  Backbone.emulateHTTP = false;
@@ -68,10 +68,10 @@
68
68
 
69
69
  // Bind an event, specified by a string name, `ev`, to a `callback` function.
70
70
  // Passing `"all"` will bind the callback to all events fired.
71
- bind : function(ev, callback) {
71
+ bind : function(ev, callback, context) {
72
72
  var calls = this._callbacks || (this._callbacks = {});
73
73
  var list = calls[ev] || (calls[ev] = []);
74
- list.push(callback);
74
+ list.push([callback, context]);
75
75
  return this;
76
76
  },
77
77
 
@@ -89,7 +89,7 @@
89
89
  var list = calls[ev];
90
90
  if (!list) return this;
91
91
  for (var i = 0, l = list.length; i < l; i++) {
92
- if (callback === list[i]) {
92
+ if (list[i] && callback === list[i][0]) {
93
93
  list[i] = null;
94
94
  break;
95
95
  }
@@ -114,7 +114,7 @@
114
114
  list.splice(i, 1); i--; l--;
115
115
  } else {
116
116
  args = both ? Array.prototype.slice.call(arguments, 1) : arguments;
117
- callback.apply(this, args);
117
+ callback[0].apply(callback[1] || this, args);
118
118
  }
119
119
  }
120
120
  }
@@ -133,7 +133,7 @@
133
133
  var defaults;
134
134
  attributes || (attributes = {});
135
135
  if (defaults = this.defaults) {
136
- if (_.isFunction(defaults)) defaults = defaults();
136
+ if (_.isFunction(defaults)) defaults = defaults.call(this);
137
137
  attributes = _.extend({}, defaults, attributes);
138
138
  }
139
139
  this.attributes = {};
@@ -641,7 +641,7 @@
641
641
  var methods = ['forEach', 'each', 'map', 'reduce', 'reduceRight', 'find', 'detect',
642
642
  'filter', 'select', 'reject', 'every', 'all', 'some', 'any', 'include',
643
643
  'contains', 'invoke', 'max', 'min', 'sortBy', 'sortedIndex', 'toArray', 'size',
644
- 'first', 'rest', 'last', 'without', 'indexOf', 'lastIndexOf', 'isEmpty'];
644
+ 'first', 'rest', 'last', 'without', 'indexOf', 'lastIndexOf', 'isEmpty', 'groupBy'];
645
645
 
646
646
  // Mix in each Underscore method as a proxy to `Collection#models`.
647
647
  _.each(methods, function(method) {
@@ -806,6 +806,8 @@
806
806
  if (this._wantsPushState && !this._hasPushState && !atRoot) {
807
807
  this.fragment = this.getFragment(null, true);
808
808
  window.location.replace(this.options.root + '#' + this.fragment);
809
+ // Return immediately as browser will do redirect to new url
810
+ return true;
809
811
  } else if (this._wantsPushState && this._hasPushState && atRoot && loc.hash) {
810
812
  this.fragment = loc.hash.replace(hashStrip, '');
811
813
  window.history.replaceState({}, document.title, loc.protocol + '//' + loc.host + this.options.root + this.fragment);
@@ -947,6 +949,7 @@
947
949
  // not `change`, `submit`, and `reset` in Internet Explorer.
948
950
  delegateEvents : function(events) {
949
951
  if (!(events || (events = this.events))) return;
952
+ if (_.isFunction(events)) events = events.call(this);
950
953
  $(this.el).unbind('.delegateEvents' + this.cid);
951
954
  for (var key in events) {
952
955
  var method = this[events[key]];
@@ -1035,8 +1038,7 @@
1035
1038
  // Default JSON-request options.
1036
1039
  var params = _.extend({
1037
1040
  type: type,
1038
- dataType: 'json',
1039
- processData: false
1041
+ dataType: 'json'
1040
1042
  }, options);
1041
1043
 
1042
1044
  // Ensure that we have a URL.
@@ -1053,7 +1055,6 @@
1053
1055
  // For older servers, emulate JSON by encoding the request into an HTML-form.
1054
1056
  if (Backbone.emulateJSON) {
1055
1057
  params.contentType = 'application/x-www-form-urlencoded';
1056
- params.processData = true;
1057
1058
  params.data = params.data ? {model : params.data} : {};
1058
1059
  }
1059
1060
 
@@ -1069,6 +1070,11 @@
1069
1070
  }
1070
1071
  }
1071
1072
 
1073
+ // Don't process data on a non-GET request.
1074
+ if (params.type !== 'GET' && ! Backbone.emulateJSON) {
1075
+ params.processData = false;
1076
+ }
1077
+
1072
1078
  // Make the request.
1073
1079
  return $.ajax(params);
1074
1080
  };
@@ -22,7 +22,6 @@
22
22
  var params = _.extend({
23
23
  type: type,
24
24
  dataType: 'json',
25
- processData: false,
26
25
  beforeSend: function( xhr ) {
27
26
  var token = $('meta[name="csrf-token"]').attr('content');
28
27
  if (token) xhr.setRequestHeader('X-CSRF-Token', token);
@@ -48,6 +47,11 @@
48
47
  params.data = JSON.stringify(data)
49
48
  }
50
49
 
50
+ // Don't process data on a non-GET request.
51
+ if (params.type !== 'GET') {
52
+ params.processData = false;
53
+ }
54
+
51
55
  // Make the request.
52
56
  return $.ajax(params);
53
57
  }
@@ -1,4 +1,4 @@
1
- // Underscore.js 1.1.6
1
+ // Underscore.js 1.1.7
2
2
  // (c) 2011 Jeremy Ashkenas, DocumentCloud Inc.
3
3
  // Underscore is freely distributable under the MIT license.
4
4
  // Portions of Underscore are inspired or borrowed from Prototype,
@@ -60,19 +60,19 @@
60
60
  }
61
61
 
62
62
  // Current version.
63
- _.VERSION = '1.1.6';
63
+ _.VERSION = '1.1.7';
64
64
 
65
65
  // Collection Functions
66
66
  // --------------------
67
67
 
68
68
  // The cornerstone, an `each` implementation, aka `forEach`.
69
- // Handles objects implementing `forEach`, arrays, and raw objects.
69
+ // Handles objects with the built-in `forEach`, arrays, and raw objects.
70
70
  // Delegates to **ECMAScript 5**'s native `forEach` if available.
71
71
  var each = _.each = _.forEach = function(obj, iterator, context) {
72
72
  if (obj == null) return;
73
73
  if (nativeForEach && obj.forEach === nativeForEach) {
74
74
  obj.forEach(iterator, context);
75
- } else if (_.isNumber(obj.length)) {
75
+ } else if (obj.length === +obj.length) {
76
76
  for (var i = 0, l = obj.length; i < l; i++) {
77
77
  if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;
78
78
  }
@@ -107,7 +107,7 @@
107
107
  return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
108
108
  }
109
109
  each(obj, function(value, index, list) {
110
- if (!initial && index === 0) {
110
+ if (!initial) {
111
111
  memo = value;
112
112
  initial = true;
113
113
  } else {
@@ -182,14 +182,14 @@
182
182
  // Delegates to **ECMAScript 5**'s native `some` if available.
183
183
  // Aliased as `any`.
184
184
  var any = _.some = _.any = function(obj, iterator, context) {
185
- iterator || (iterator = _.identity);
185
+ iterator = iterator || _.identity;
186
186
  var result = false;
187
187
  if (obj == null) return result;
188
188
  if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
189
189
  each(obj, function(value, index, list) {
190
- if (result = iterator.call(context, value, index, list)) return breaker;
190
+ if (result |= iterator.call(context, value, index, list)) return breaker;
191
191
  });
192
- return result;
192
+ return !!result;
193
193
  };
194
194
 
195
195
  // Determine if a given value is included in the array or object using `===`.
@@ -327,23 +327,34 @@
327
327
 
328
328
  // Return a version of the array that does not contain the specified value(s).
329
329
  _.without = function(array) {
330
- var values = slice.call(arguments, 1);
331
- return _.filter(array, function(value){ return !_.include(values, value); });
330
+ return _.difference(array, slice.call(arguments, 1));
332
331
  };
333
332
 
334
333
  // Produce a duplicate-free version of the array. If the array has already
335
334
  // been sorted, you have the option of using a faster algorithm.
336
335
  // Aliased as `unique`.
337
- _.uniq = _.unique = function(array, isSorted) {
338
- return _.reduce(array, function(memo, el, i) {
339
- if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) memo[memo.length] = el;
336
+ _.uniq = _.unique = function(array, isSorted, iterator) {
337
+ var initial = iterator ? _.map(array, iterator) : array;
338
+ var result = [];
339
+ _.reduce(initial, function(memo, el, i) {
340
+ if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) {
341
+ memo[memo.length] = el;
342
+ result[result.length] = array[i];
343
+ }
340
344
  return memo;
341
345
  }, []);
346
+ return result;
347
+ };
348
+
349
+ // Produce an array that contains the union: each distinct element from all of
350
+ // the passed-in arrays.
351
+ _.union = function() {
352
+ return _.uniq(_.flatten(arguments));
342
353
  };
343
354
 
344
355
  // Produce an array that contains every item shared between all the
345
- // passed-in arrays.
346
- _.intersect = function(array) {
356
+ // passed-in arrays. (Aliased as "intersect" for back-compat.)
357
+ _.intersection = _.intersect = function(array) {
347
358
  var rest = slice.call(arguments, 1);
348
359
  return _.filter(_.uniq(array), function(item) {
349
360
  return _.every(rest, function(other) {
@@ -352,6 +363,12 @@
352
363
  });
353
364
  };
354
365
 
366
+ // Take the difference between one array and another.
367
+ // Only the elements present in just the first array will remain.
368
+ _.difference = function(array, other) {
369
+ return _.filter(array, function(value){ return !_.include(other, value); });
370
+ };
371
+
355
372
  // Zip together multiple lists into a single array -- elements that share
356
373
  // an index go together.
357
374
  _.zip = function() {
@@ -548,7 +565,11 @@
548
565
  // Return a sorted list of the function names available on the object.
549
566
  // Aliased as `methods`
550
567
  _.functions = _.methods = function(obj) {
551
- return _.filter(_.keys(obj), function(key){ return _.isFunction(obj[key]); }).sort();
568
+ var names = [];
569
+ for (var key in obj) {
570
+ if (_.isFunction(obj[key])) names.push(key);
571
+ }
572
+ return names.sort();
552
573
  };
553
574
 
554
575
  // Extend a given object with all the properties in passed-in object(s).
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rails-backbone
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.5.1
5
+ version: 0.5.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ryan Fitzgerald
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-07-08 00:00:00 -04:00
14
+ date: 2011-08-07 00:00:00 -04:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
@@ -21,7 +21,7 @@ dependencies:
21
21
  requirements:
22
22
  - - ~>
23
23
  - !ruby/object:Gem::Version
24
- version: 3.1.0.rc4
24
+ version: 3.1.0.rc5
25
25
  type: :runtime
26
26
  prerelease: false
27
27
  version_requirements: *id001
@@ -103,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
103
103
  requirements:
104
104
  - - ">="
105
105
  - !ruby/object:Gem::Version
106
- hash: -4012719302354285017
106
+ hash: -2957060880255582041
107
107
  segments:
108
108
  - 0
109
109
  version: "0"
@@ -112,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  requirements:
113
113
  - - ">="
114
114
  - !ruby/object:Gem::Version
115
- hash: -4012719302354285017
115
+ hash: -2957060880255582041
116
116
  segments:
117
117
  - 0
118
118
  version: "0"