Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 3 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
Commits on Feb 08, 2012
Brian Takita v0.10.0 bb65e6e
Brian Takita Jelly.Observers.attach can now take an array of arrays.
Added jelly_attachment_array.
attach_javascript_component now uses jelly_attachment_array.
f9a8c6e
Brian Takita v0.11.0 1d67607
View
38 generators/jelly/templates/javascripts/jelly.js
@@ -1,7 +1,7 @@
/**
* Jelly. a sweet unobtrusive javascript framework for Rails
*
- * version 0.8.13
+ * version 0.11.0
*
* Copyright (c) 2009 Pivotal Labs
* Licensed under the MIT license.
@@ -42,22 +42,36 @@
if (this === Jelly) {
return Jelly.Observers.attach.apply(this.observers, arguments);
}
+ var self = Jelly.Observers;
for (var i = 0; i < arguments.length; i++) {
var definitionOrComponent = arguments[i];
if (definitionOrComponent.component) {
- var component = Jelly.Observers.evaluateComponent(definitionOrComponent.component);
- if (component.init) {
- var initReturnValue = component.init.apply(component, definitionOrComponent.arguments);
- if (initReturnValue === false || initReturnValue === null) {
- } else {
- Jelly.Observers.pushIfObserver.call(this, initReturnValue || component);
- }
- } else {
- Jelly.Observers.pushIfObserver.call(this, component);
- }
+ self.attachFromDefinition.call(
+ this,
+ self.evaluateComponent(definitionOrComponent.component),
+ definitionOrComponent.arguments
+ );
+ } else if (Object.prototype.toString.call(definitionOrComponent) === "[object Array]") {
+ self.attachFromDefinition.call(
+ this,
+ self.evaluateComponent(definitionOrComponent[0]),
+ definitionOrComponent.slice(1)
+ );
+ } else {
+ self.pushIfObserver.call(this, Jelly.Observers.evaluateComponent(definitionOrComponent));
+ }
+ }
+ },
+
+ attachFromDefinition:function (component, args) {
+ if (component.init) {
+ var initReturnValue = component.init.apply(component, args);
+ if (initReturnValue === false || initReturnValue === null) {
} else {
- Jelly.Observers.pushIfObserver.call(this, Jelly.Observers.evaluateComponent(definitionOrComponent));
+ Jelly.Observers.pushIfObserver.call(this, initReturnValue || component);
}
+ } else {
+ Jelly.Observers.pushIfObserver.call(this, component);
}
},
View
2  jelly.gemspec
@@ -5,7 +5,7 @@
Gem::Specification.new do |s|
s.name = %q{honkster-jelly}
- s.version = "0.9.3"
+ s.version = "0.11.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Pivotal Labs, Inc"]
View
4 lib/jelly/common.rb
@@ -20,5 +20,9 @@ def jelly_notify_attach_hash (components=jelly_attachments)
def jelly_attachment_hash(component_name, *args)
{'component' => component_name, 'arguments' => args}
end
+
+ def jelly_attachment_array(component_name, *args)
+ [component_name, *args]
+ end
end
end
View
14 lib/jelly/jelly_helper.rb
@@ -14,18 +14,10 @@ def application_jelly_files(jelly_files_path_from_javascripts = '', rails_root =
def spread_jelly
attach_javascript_component("Jelly.Location")
attach_javascript_component("Jelly.Page", controller.controller_path.camelcase, controller.action_name)
- <<-HTML
- #{window_token_javascript_tag}
- #{attach_javascript_component_javascript_tag(jelly_attachments)}
- HTML
+ attach_javascript_component_javascript_tag(jelly_attachments)
end
- def window_token_javascript_tag
- javascript_tag("window._token = '#{form_authenticity_token}';")
- end
-
- def attach_javascript_component_javascript_tag(*components)
- components = [components].flatten
+ def attach_javascript_component_javascript_tag(components)
javascript_tag <<-JS
jQuery(document).ready(function() {
Jelly.attach.apply(Jelly, #{components.to_json});
@@ -38,7 +30,7 @@ def clear_jelly_attached
end
def attach_javascript_component(component_name, *args)
- key = jelly_attachment_hash(component_name, *args)
+ key = jelly_attachment_array(component_name, *args)
unless jelly_attachments.include? key
jelly_attachments << key
end
View
123 spec/javascript/jelly_spec.js
@@ -155,6 +155,129 @@ describe("Jelly", function() {
});
});
+ describe("when the argument is an array", function() {
+ describe("when the component does not respond to init", function() {
+ describe("when the component is referenced as a String", function() {
+ beforeEach(function() {
+ window.MyComponent = {
+ };
+ });
+
+ afterEach(function() {
+ delete window.MyComponent;
+ });
+
+ it("attaches the component to Jelly.observers", function() {
+ Jelly.attach(["MyComponent"]);
+ expect(Jelly.observers).toContain(MyComponent);
+ });
+ });
+
+ describe("when the component is referenced as itself", function() {
+ it("attaches the component to Jelly.observers", function() {
+ var component = {};
+ Jelly.attach([component]);
+ expect(Jelly.observers).toContain(component);
+ });
+ });
+ });
+
+ describe("when component responds to init", function() {
+ describe("when the component's init method returns undefined", function() {
+ describe("when the component is referenced as a String", function() {
+ beforeEach(function() {
+ window.MyComponent = {
+ init: function() {
+ }
+ };
+ });
+
+ afterEach(function() {
+ delete window.MyComponent;
+ });
+
+ it("calls the init method on the component and attaches the component to Jelly.observers", function() {
+ spyOn(MyComponent, 'init');
+ Jelly.attach([MyComponent, 1, 2]);
+ expect(MyComponent.init).wasCalledWith(1, 2);
+ expect(Jelly.observers).toContain(MyComponent);
+ });
+ });
+
+ describe("when the component is referenced as itself", function() {
+ var component;
+ beforeEach(function() {
+ component = {
+ init: function() {
+ }
+ };
+ });
+
+ it("calls the init method on the component and attaches the component to Jelly.observers", function() {
+ spyOn(component, 'init');
+ Jelly.attach([component, 1, 2]);
+ expect(component.init).wasCalledWith(1, 2);
+ expect(Jelly.observers).toContain(component);
+ });
+ });
+ });
+
+ describe("when the component's init method returns false", function() {
+ var component;
+ beforeEach(function() {
+ component = {
+ init: function() {
+ component.initCalled = true;
+ return false;
+ }
+ };
+ });
+
+ it("calls the init method on the component and does not attaches an observer to Jelly.observers", function() {
+ var originalObserversLength = Jelly.observers.length;
+ Jelly.attach([component, 1, 2]);
+ expect(component.initCalled).toBeTruthy();
+ expect(Jelly.observers.length).toEqual(originalObserversLength);
+ expect(Jelly.observers).toNotContain(component);
+ });
+ });
+
+ describe("when the component's init method returns null", function() {
+ var component;
+ beforeEach(function() {
+ component = {
+ init: function() {
+ component.initCalled = true;
+ return null;
+ }
+ };
+ });
+
+ it("calls the init method on the component and does not attaches an observer to Jelly.observers", function() {
+ var originalObserversLength = Jelly.observers.length;
+ Jelly.attach([component, 1, 2]);
+ expect(component.initCalled).toBeTruthy();
+ expect(Jelly.observers.length).toEqual(originalObserversLength);
+ expect(Jelly.observers).toNotContain(component);
+ });
+ });
+
+ describe("when the component's init method returns an object", function() {
+ it("attaches the returned object (instead of the component) to Jelly.observers", function() {
+ var observer = new Object();
+ var component = {
+ init: function() {
+ return observer;
+ }
+ };
+ Jelly.attach([component, 1, 2]);
+ expect(Jelly.observers).toContain(observer);
+ expect(Jelly.observers).toNotContain(component);
+ });
+ });
+ });
+ });
+
describe("when the argument does not contain a 'component' key", function() {
describe("when the component is referenced as a String", function() {
beforeEach(function() {
View
17 spec/jelly/jelly_helper_spec.rb
@@ -13,16 +13,15 @@ def jelly_attach_arguments(html)
controller.action_name {'super_good_action'}
end
stub(helper).controller {stub_controller}
- mock(helper).form_authenticity_token {'areallysecuretoken'}
end
it "should create a javascript include tag that attaches the Jelly.Location and Jelly.Page components" do
output = helper.spread_jelly
output.should include('<script type="text/javascript">')
doc = Nokogiri::HTML(output)
- argument = jelly_attach_arguments(doc.css("script")[1].inner_html)
- argument.should include({'component' => "Jelly.Location", 'arguments' => []})
- argument.should include({'component' => "Jelly.Page", 'arguments' => ['MyFunController', 'super_good_action']})
+ argument = jelly_attach_arguments(doc.at("script").inner_html)
+ argument.should include(["Jelly.Location"])
+ argument.should include(["Jelly.Page", 'MyFunController', 'super_good_action'])
end
end
@@ -62,8 +61,8 @@ def helper.form_authenticity_token
helper.attach_javascript_component("MyComponent", 'arg1', 'arg2', 'arg3')
helper.attach_javascript_component("MyComponent", 'arg1', 'arg2', 'arg5')
helper.instance_variable_get(:@jelly_attachments).should == [
- {'component' => "MyComponent", 'arguments' => ['arg1', 'arg2', 'arg3']},
- {'component' => "MyComponent", 'arguments' => ['arg1', 'arg2', 'arg5']},
+ ["MyComponent", 'arg1', 'arg2', 'arg3'],
+ ["MyComponent", 'arg1', 'arg2', 'arg5'],
]
end
@@ -71,16 +70,16 @@ def helper.form_authenticity_token
helper.attach_javascript_component("MyComponent", 'arg1', 'arg2', 'arg3')
expected_args = ['arg1','arg2','arg3'].to_json
helper.instance_variable_get(:@jelly_attachments).should == [
- {'component' => "MyComponent", 'arguments' => ['arg1', 'arg2', 'arg3']}
+ ["MyComponent", 'arg1', 'arg2', 'arg3']
]
html = helper.spread_jelly
doc = Nokogiri::HTML(html)
- document_ready_tag = doc.css("script")[1]
+ document_ready_tag = doc.at("script")
document_ready_tag.inner_html.should include("jQuery(document).ready(function() {")
document_ready_part = document_ready_tag.inner_html.split("\n")[3]
arguments = jelly_attach_arguments(document_ready_part)
- arguments.should include({'component' => "MyComponent", 'arguments' => ['arg1', 'arg2', 'arg3']})
+ arguments.should include(["MyComponent", 'arg1', 'arg2', 'arg3'])
end
end

No commit comments for this range

Something went wrong with that request. Please try again.