requirejs-controllers 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/.rvmrc +1 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +71 -0
- data/LICENSE +22 -0
- data/README.md +70 -0
- data/Rakefile +2 -0
- data/app/helpers/requirejs_controllers_helper.rb +15 -0
- data/bootstrap.sh +2 -0
- data/lib/requirejs-controllers.rb +1 -0
- data/lib/requirejs/controllers/rails.rb +7 -0
- data/lib/requirejs/controllers/rails/engine.rb +9 -0
- data/lib/requirejs/controllers/rails/version.rb +7 -0
- data/requirejs-controllers.gemspec +23 -0
- data/vendor/assets/javascripts/controllers/base.js.coffee +48 -0
- data/vendor/assets/javascripts/controllers/dataview.js.coffee +108 -0
- data/vendor/assets/javascripts/controllers/grid.js.coffee +15 -0
- metadata +106 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg/
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use --create ruby-1.9.3-p125@requirejs-controllers
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
requirejs-controllers (0.1.0)
|
5
|
+
coffee-rails
|
6
|
+
railties (~> 3.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionpack (3.2.2)
|
12
|
+
activemodel (= 3.2.2)
|
13
|
+
activesupport (= 3.2.2)
|
14
|
+
builder (~> 3.0.0)
|
15
|
+
erubis (~> 2.7.0)
|
16
|
+
journey (~> 1.0.1)
|
17
|
+
rack (~> 1.4.0)
|
18
|
+
rack-cache (~> 1.1)
|
19
|
+
rack-test (~> 0.6.1)
|
20
|
+
sprockets (~> 2.1.2)
|
21
|
+
activemodel (3.2.2)
|
22
|
+
activesupport (= 3.2.2)
|
23
|
+
builder (~> 3.0.0)
|
24
|
+
activesupport (3.2.2)
|
25
|
+
i18n (~> 0.6)
|
26
|
+
multi_json (~> 1.0)
|
27
|
+
builder (3.0.0)
|
28
|
+
coffee-rails (3.2.2)
|
29
|
+
coffee-script (>= 2.2.0)
|
30
|
+
railties (~> 3.2.0)
|
31
|
+
coffee-script (2.2.0)
|
32
|
+
coffee-script-source
|
33
|
+
execjs
|
34
|
+
coffee-script-source (1.2.0)
|
35
|
+
erubis (2.7.0)
|
36
|
+
execjs (1.3.0)
|
37
|
+
multi_json (~> 1.0)
|
38
|
+
hike (1.2.1)
|
39
|
+
i18n (0.6.0)
|
40
|
+
journey (1.0.3)
|
41
|
+
json (1.6.5)
|
42
|
+
multi_json (1.1.0)
|
43
|
+
rack (1.4.1)
|
44
|
+
rack-cache (1.2)
|
45
|
+
rack (>= 0.4)
|
46
|
+
rack-ssl (1.3.2)
|
47
|
+
rack
|
48
|
+
rack-test (0.6.1)
|
49
|
+
rack (>= 1.0)
|
50
|
+
railties (3.2.2)
|
51
|
+
actionpack (= 3.2.2)
|
52
|
+
activesupport (= 3.2.2)
|
53
|
+
rack-ssl (~> 1.3.2)
|
54
|
+
rake (>= 0.8.7)
|
55
|
+
rdoc (~> 3.4)
|
56
|
+
thor (~> 0.14.6)
|
57
|
+
rake (0.9.2.2)
|
58
|
+
rdoc (3.12)
|
59
|
+
json (~> 1.4)
|
60
|
+
sprockets (2.1.2)
|
61
|
+
hike (~> 1.2)
|
62
|
+
rack (~> 1.0)
|
63
|
+
tilt (~> 1.1, != 1.3.0)
|
64
|
+
thor (0.14.6)
|
65
|
+
tilt (1.3.3)
|
66
|
+
|
67
|
+
PLATFORMS
|
68
|
+
ruby
|
69
|
+
|
70
|
+
DEPENDENCIES
|
71
|
+
requirejs-controllers!
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Beneidkt Böhm <bb@xnull.de>
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# RequireJS Controllers
|
2
|
+
|
3
|
+
requirejs-controllers is a simple gem that provides a bunch of RequireJS
|
4
|
+
modules/classes useful to build JavaScript controllers for your Rails
|
5
|
+
controllers.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'requirejs-controllers'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install requirejs-controllers
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Simply add the `requirejs_controller_tag` to your view to load the controller
|
24
|
+
specific module:
|
25
|
+
|
26
|
+
= requirejs_controller_tag
|
27
|
+
|
28
|
+
The above helper will add a `requirejs_include_tag` and a simple `main` module
|
29
|
+
to load the controller specifics:
|
30
|
+
|
31
|
+
<script>var require = {"baseUrl":"/assets"};</script>
|
32
|
+
<script src="/assets/require.js"></script>
|
33
|
+
<script>
|
34
|
+
define('main', function() {
|
35
|
+
require(['controllers/users'], function(controller) {
|
36
|
+
window.controller = new controller('index')
|
37
|
+
})
|
38
|
+
})
|
39
|
+
</script>
|
40
|
+
|
41
|
+
## Controllers
|
42
|
+
|
43
|
+
The following controllers are shipped with `requirejs-controllers`.
|
44
|
+
|
45
|
+
### BaseController
|
46
|
+
|
47
|
+
The `BaseController` is the base class for all controllers (similar to Rails'
|
48
|
+
`ApplicationController`). The `BaseController` will bind all events specified
|
49
|
+
in subclasses (see below) and then call the class method for the current
|
50
|
+
controller action if such a method exists in your class.
|
51
|
+
|
52
|
+
### GridController
|
53
|
+
|
54
|
+
tbd
|
55
|
+
|
56
|
+
### DataViewController
|
57
|
+
|
58
|
+
tbd
|
59
|
+
|
60
|
+
## Event Binding
|
61
|
+
|
62
|
+
tbd
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
1. Fork it
|
67
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
68
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
69
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
70
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module RequirejsControllersHelper
|
2
|
+
def requirejs_controller_tag
|
3
|
+
html = requirejs_include_tag
|
4
|
+
html.concat <<-HTML.html_safe
|
5
|
+
<script>
|
6
|
+
define('main', function() {
|
7
|
+
require(['controllers/#{controller.controller_path}'], function(controller) {
|
8
|
+
window.controller = new controller('#{controller.action_name}')
|
9
|
+
})
|
10
|
+
})
|
11
|
+
</script>
|
12
|
+
HTML
|
13
|
+
html.html_safe
|
14
|
+
end
|
15
|
+
end
|
data/bootstrap.sh
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "requirejs/controllers/rails"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/requirejs/controllers/rails/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = "requirejs-controllers"
|
6
|
+
gem.version = Requirejs::Controllers::Rails::VERSION
|
7
|
+
gem.authors = ["Benedikt Böhm"]
|
8
|
+
gem.email = ["benedikt.boehm@madvertise.com"]
|
9
|
+
gem.description = %q{A collection of RequireJS controllers for Rails 3.x}
|
10
|
+
gem.summary = %q{A collection of RequireJS controllers for Rails 3.x}
|
11
|
+
gem.homepage = "https://github.com/madvertise/requirejs-controllers"
|
12
|
+
|
13
|
+
gem.add_dependency "railties", "~> 3.0"
|
14
|
+
|
15
|
+
gem.add_dependency "coffee-rails"
|
16
|
+
gem.add_dependency "jquery-plugins-rails"
|
17
|
+
gem.add_dependency "requirejs-rails"
|
18
|
+
|
19
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
gem.files = `git ls-files`.split("\n")
|
21
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
gem.require_paths = ["lib"]
|
23
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
define ->
|
2
|
+
|
3
|
+
class BaseController
|
4
|
+
constructor: (action) ->
|
5
|
+
# bind events from subclasses
|
6
|
+
for event in ['click', 'focus', 'blur', 'change', 'keyup', 'submit']
|
7
|
+
for selector, params of (this[event]? && this[event]())
|
8
|
+
@bindevent selector, event, params
|
9
|
+
|
10
|
+
# call action if it exists
|
11
|
+
@action = action
|
12
|
+
this[action]() if this[action]?
|
13
|
+
|
14
|
+
bindevent: (selector, event, params) ->
|
15
|
+
# extract function from params
|
16
|
+
if $.type(params) == 'function'
|
17
|
+
# direct call
|
18
|
+
fnc = params
|
19
|
+
else
|
20
|
+
# at first parse the selector for restrictions
|
21
|
+
restricted_to = params
|
22
|
+
[selector, action_restriction] = selector.split('@')
|
23
|
+
if action_restriction?
|
24
|
+
if action_restriction == 'modify'
|
25
|
+
restricted_to = ['create', 'update', 'edit', 'new']
|
26
|
+
else
|
27
|
+
restricted_to = [action_restriction]
|
28
|
+
return unless @action in restricted_to
|
29
|
+
fnc = params[params.length-1]
|
30
|
+
|
31
|
+
# wrap our callback with event params
|
32
|
+
wrapped = (e) =>
|
33
|
+
fnc.apply(this, [$(e.target), e])
|
34
|
+
|
35
|
+
# bind event to current and future selectors
|
36
|
+
$(selector).livequery(event, wrapped)
|
37
|
+
|
38
|
+
# syntax sugar for bindevent callbacks
|
39
|
+
at: (page, fnc) ->
|
40
|
+
[page, fnc]
|
41
|
+
|
42
|
+
on: (params...) ->
|
43
|
+
params
|
44
|
+
|
45
|
+
on_modify: (fnc) ->
|
46
|
+
['create', 'update', 'edit', 'new', fnc]
|
47
|
+
|
48
|
+
return BaseController
|
@@ -0,0 +1,108 @@
|
|
1
|
+
define [
|
2
|
+
'controllers/grid'
|
3
|
+
], (GridController) ->
|
4
|
+
|
5
|
+
class DataViewController extends GridController
|
6
|
+
|
7
|
+
initDataView: (@selector, @columns, @options, @model=null) ->
|
8
|
+
@dataView = new Slick.Data.DataView()
|
9
|
+
@grid = new Slick.Grid(@selector, @dataView, @columns, @options)
|
10
|
+
@columnpicker = new Slick.Controls.ColumnPicker(@columns, @grid, @options)
|
11
|
+
|
12
|
+
@grid.onSort.subscribe (e, args) =>
|
13
|
+
@dataView.sort(new Slick.Sorters.Sorter(args), true)
|
14
|
+
@grid.invalidateAllRows()
|
15
|
+
@grid.render()
|
16
|
+
|
17
|
+
@dataView.onRowCountChanged.subscribe (e, args) =>
|
18
|
+
@grid.updateRowCount()
|
19
|
+
@grid.render()
|
20
|
+
|
21
|
+
@dataView.onRowsChanged.subscribe (e, args) =>
|
22
|
+
@grid.invalidateRows(args)
|
23
|
+
@grid.render()
|
24
|
+
|
25
|
+
initLoader: ->
|
26
|
+
@model.onDataLoading.subscribe (e, args) =>
|
27
|
+
@showIndicator()
|
28
|
+
|
29
|
+
@model.onDataLoaded.subscribe (e, args) =>
|
30
|
+
@hideIndicator()
|
31
|
+
|
32
|
+
@model.onDataLoadedSuccess.subscribe (e, args) =>
|
33
|
+
@dataView.beginUpdate()
|
34
|
+
@dataView.setItems(args.data)
|
35
|
+
@dataView.endUpdate()
|
36
|
+
@grid.invalidateAllRows()
|
37
|
+
@grid.render()
|
38
|
+
|
39
|
+
@model.onDataLoadedError.subscribe (e, args) =>
|
40
|
+
alert("failed to load data from server")
|
41
|
+
|
42
|
+
initWriter: ->
|
43
|
+
@model.onDataWriting.subscribe (e, args) =>
|
44
|
+
@showIndicator("Writing")
|
45
|
+
|
46
|
+
@model.onDataWritten.subscribe (e, args) =>
|
47
|
+
@hideIndicator()
|
48
|
+
|
49
|
+
@model.onDataWrittenSuccess.subscribe (e, args) =>
|
50
|
+
@dataView.updateItem(args.item.id, args.data)
|
51
|
+
@grid.updateRow(args.row)
|
52
|
+
|
53
|
+
@model.onDataWrittenError.subscribe (e, args) =>
|
54
|
+
alert("failed to write data on server")
|
55
|
+
|
56
|
+
@grid.onCellChange.subscribe (e, args) =>
|
57
|
+
@model.writeData(args)
|
58
|
+
|
59
|
+
showIndicator: (label="Loading") ->
|
60
|
+
$grid = $(@selector)
|
61
|
+
@loadingIndicator = $("<span class='loading-indicator'><label>#{label}...</label></span>").appendTo(document.body)
|
62
|
+
@loadingIndicator.css("position", "absolute")
|
63
|
+
.css("top", $grid.position().top + $grid.height() / 2 - @loadingIndicator.height() / 2)
|
64
|
+
.css("left", $grid.position().left + $grid.width() / 2 - @loadingIndicator.width() / 2)
|
65
|
+
@loadingIndicator.show()
|
66
|
+
|
67
|
+
hideIndicator: ->
|
68
|
+
@loadingIndicator.fadeOut()
|
69
|
+
@loadingIndicator.remove()
|
70
|
+
|
71
|
+
initHeaderRow: ->
|
72
|
+
if @options.showHeaderRow
|
73
|
+
$(@grid.getHeaderRow()).delegate ":input", "change keyup", (e) =>
|
74
|
+
@columnFilters[$(e.target).data("column-id")] = $.trim($(e.target).val())
|
75
|
+
@dataView.refresh()
|
76
|
+
|
77
|
+
@grid.onColumnsReordered.subscribe (e, args) =>
|
78
|
+
@updateHeaderRow()
|
79
|
+
|
80
|
+
@grid.onColumnsResized.subscribe (e, args) =>
|
81
|
+
@updateHeaderRow()
|
82
|
+
|
83
|
+
@dataView.beginUpdate()
|
84
|
+
@dataView.setFilter(@filterHeaderRow)
|
85
|
+
@dataView.endUpdate()
|
86
|
+
|
87
|
+
@updateHeaderRow()
|
88
|
+
|
89
|
+
updateHeaderRow: ->
|
90
|
+
@columnFilters ?= {}
|
91
|
+
|
92
|
+
for column in @columns
|
93
|
+
if not column.noHeaderFilter
|
94
|
+
header = @grid.getHeaderRowColumn(column.id)
|
95
|
+
$(header).empty()
|
96
|
+
$("<input type='text'>")
|
97
|
+
.data("column-id", column.id)
|
98
|
+
.val(@columnFilters[column.id])
|
99
|
+
.appendTo(header)
|
100
|
+
|
101
|
+
filterHeaderRow: (item) =>
|
102
|
+
for column, value of @columnFilters
|
103
|
+
column = @grid.getColumns()[@grid.getColumnIndex(column)]
|
104
|
+
if not @headerFilter(column, value, item)
|
105
|
+
return false
|
106
|
+
return true
|
107
|
+
|
108
|
+
return DataViewController
|
@@ -0,0 +1,15 @@
|
|
1
|
+
define [
|
2
|
+
'controllers/base'
|
3
|
+
], (BaseController) ->
|
4
|
+
|
5
|
+
class GridController extends BaseController
|
6
|
+
|
7
|
+
buildColumns: (columns, _locals) ->
|
8
|
+
for column in columns
|
9
|
+
$.extend(column, {
|
10
|
+
field: column.id
|
11
|
+
name: _locals[column.id]
|
12
|
+
sortable: if column.sortable? then column.sortable else true
|
13
|
+
})
|
14
|
+
|
15
|
+
return GridController
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: requirejs-controllers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Benedikt Böhm
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: railties
|
16
|
+
requirement: &9001160 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *9001160
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: coffee-rails
|
27
|
+
requirement: &9000440 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *9000440
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: jquery-plugins-rails
|
38
|
+
requirement: &8999660 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *8999660
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: requirejs-rails
|
49
|
+
requirement: &8999180 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *8999180
|
58
|
+
description: A collection of RequireJS controllers for Rails 3.x
|
59
|
+
email:
|
60
|
+
- benedikt.boehm@madvertise.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- .rvmrc
|
67
|
+
- Gemfile
|
68
|
+
- Gemfile.lock
|
69
|
+
- LICENSE
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- app/helpers/requirejs_controllers_helper.rb
|
73
|
+
- bootstrap.sh
|
74
|
+
- lib/requirejs-controllers.rb
|
75
|
+
- lib/requirejs/controllers/rails.rb
|
76
|
+
- lib/requirejs/controllers/rails/engine.rb
|
77
|
+
- lib/requirejs/controllers/rails/version.rb
|
78
|
+
- requirejs-controllers.gemspec
|
79
|
+
- vendor/assets/javascripts/controllers/base.js.coffee
|
80
|
+
- vendor/assets/javascripts/controllers/dataview.js.coffee
|
81
|
+
- vendor/assets/javascripts/controllers/grid.js.coffee
|
82
|
+
homepage: https://github.com/madvertise/requirejs-controllers
|
83
|
+
licenses: []
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 1.8.17
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: A collection of RequireJS controllers for Rails 3.x
|
106
|
+
test_files: []
|