kolo 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@ class ViewModel
5
5
  @flashMessage.subscribe (newValue)=>
6
6
  if (newValue? && newValue != '')
7
7
  setTimeout =>
8
- @flashMessage('')
8
+ @flashMessage ''
9
9
  , 10000
10
10
  return true
11
11
 
@@ -13,7 +13,7 @@ class ViewModel
13
13
  @errorMessage.subscribe (newValue)=>
14
14
  if (newValue? && newValue != '')
15
15
  setTimeout =>
16
- @errorMessage('')
16
+ @errorMessage ''
17
17
  , 10000
18
18
  return true
19
19
 
@@ -21,10 +21,6 @@ class ViewModel
21
21
  $('body').attr("data-#{name}", value)
22
22
  @loading(/ing$/.test(value))
23
23
 
24
- class ClientViewModel extends ViewModel
25
-
26
- class AdminViewModel extends ViewModel
27
-
28
24
  # Construct passing in a name (which is used in notifications) and a URL (eg /api/v1/people.json) that is used to load items (GET) and create them (POST)
29
25
  # Plus an optional pushStateURI; if supplied (for example as /path/to/object) then the pushState is updated to selected().name() with the URI set to /path/to/object/#{selected().id}
30
26
  #
@@ -42,24 +38,21 @@ class AdminViewModel extends ViewModel
42
38
  #
43
39
  # After instantiation you can also set the following hooks:
44
40
  # db.sortFunction = function(a, b) { ... }
45
- # db.onBeforeLoad = function() { ... }
46
- # db.onAfterLoad = function() { ... }
47
41
 
48
42
  class Db
49
- constructor: (@name, url, @pushStateUri)->
43
+ constructor: (@viewModel, @name, url, @pushStateUri)->
50
44
  @url = ko.observable url
45
+ @createUrl = ko.observable url
51
46
  @items = ko.observableArray []
52
47
  @selected = ko.observable null
53
48
  @plural = "#{@name}s"
54
49
  @sortFunction = null
55
- @onBeforeLoad = null
56
- @onAfterLoad = null
57
- @onAfterPost = null
58
- @onAfterDelete = null
59
50
  @autoLoading = false
51
+
60
52
  @url.subscribe (newValue)=>
61
53
  if newValue?
62
54
  @load(false)
55
+
63
56
  if @pushStateUri? && history.pushState?
64
57
  @selected.subscribe (newValue)=>
65
58
  if newValue? and newValue.id?
@@ -86,51 +79,47 @@ class Db
86
79
  load: (autoReload = false, afterLoad = null)=>
87
80
  return unless @canLoad()
88
81
  if !@selected()
89
- viewModel.systemNotification @plural, 'loading'
90
- viewModel.loading true
91
- @onBeforeLoad() if @onBeforeLoad?
82
+ @viewModel.systemNotification @plural, 'loading'
83
+ @viewModel.loading true
92
84
  $.get @url(), (data)=>
93
85
  @doLoad(data)
94
- @onAfterLoad() if @onAfterLoad?
95
- afterLoad() if afterLoad?
96
- viewModel.loading false
97
- viewModel.systemNotification @plural, 'loaded'
86
+ afterLoad(data) if afterLoad?
87
+ @viewModel.loading false
88
+ @viewModel.systemNotification @plural, 'loaded'
98
89
  if autoReload
99
90
  @autoLoading = true
100
91
  setTimeout =>
101
- @load(true)
92
+ @load(true, afterLoad)
102
93
  , 30000
103
94
  return false
104
95
 
105
- postTo: (url, data, callback)->
106
- viewModel.systemNotification @name, 'saving'
107
- viewModel.loading true
96
+ postTo: (url, data, afterPost)->
97
+ @viewModel.systemNotification @name, 'saving'
98
+ @viewModel.loading true
108
99
  $.ajax
109
100
  url: url
110
101
  dataType: 'json'
111
102
  type: 'POST'
112
103
  data: data
113
104
  success: (data)=>
114
- viewModel.systemNotification @name, 'saved'
115
- viewModel.loading false
116
- callback(data) if callback?
117
- @onAfterPost(data) if @onAfterPost?
105
+ afterPost(data) if afterPost?
106
+ @viewModel.loading false
107
+ @viewModel.systemNotification @name, 'saved'
118
108
  @load(false)
119
-
120
109
  return true
121
110
 
122
111
  add: =>
123
112
  itemToAdd = @newItem(null)
124
113
  @selected(itemToAdd)
125
- viewModel.systemNotification @name, 'new'
114
+ @viewModel.systemNotification @name, 'new'
126
115
  return itemToAdd
127
116
 
128
- save: (item)=>
129
- return unless item.valid()
117
+ save: (item, afterSave, onError)=>
118
+ onError() if !item.valid() && onError?
130
119
  if item.id?
131
- @doUpdate item
120
+ @doUpdate item, afterSave
132
121
  else
133
- @doCreate item
122
+ @doCreate item, afterSave
134
123
 
135
124
  doLoad: (data)=>
136
125
  for itemData in @itemDataFrom(data)
@@ -139,24 +128,25 @@ class Db
139
128
  if @sortFunction?
140
129
  @items.sort @sortFunction
141
130
 
142
- doCreate: (item)=>
143
- viewModel.systemNotification @name, 'saving'
144
- viewModel.loading true
131
+ doCreate: (item, afterSave)=>
132
+ @viewModel.systemNotification @name, 'saving'
133
+ @viewModel.loading true
145
134
  $.ajax
146
- url: @url()
135
+ url: @createUrl()
147
136
  dataType: 'json'
148
137
  type: 'POST'
149
138
  data: @toJS(item)
150
139
  success: (data)=>
151
140
  @selected null
152
- viewModel.systemNotification @name, 'saved'
153
- viewModel.loading false
141
+ afterSave(item) if afterSave?
142
+ @viewModel.systemNotification @name, 'saved'
143
+ @viewModel.loading false
154
144
  @load()
155
145
  return false
156
146
 
157
- doUpdate: (item)=>
158
- viewModel.systemNotification @name, 'saving'
159
- viewModel.loading true
147
+ doUpdate: (item, afterSave)=>
148
+ @viewModel.systemNotification @name, 'saving'
149
+ @viewModel.loading true
160
150
  $.ajax
161
151
  url: @urlFor(item)
162
152
  dataType: 'json'
@@ -164,24 +154,25 @@ class Db
164
154
  data: @toJS(item)
165
155
  success: (data)=>
166
156
  @selected null
167
- viewModel.systemNotification @name, 'saved'
168
- viewModel.loading false
157
+ afterSave(item) if afterSave?
158
+ @viewModel.systemNotification @name, 'saved'
159
+ @viewModel.loading false
169
160
  @load()
170
161
  return false
171
162
 
172
- doDestroy: (item)=>
173
- viewModel.systemNotification @name, 'deleting'
174
- viewModel.loading true
163
+ doDestroy: (item, afterDelete)=>
164
+ @viewModel.systemNotification @name, 'deleting'
165
+ @viewModel.loading true
175
166
  $.ajax
176
167
  url: @urlFor(item)
177
168
  dataType: 'json'
178
169
  type: 'DELETE'
179
170
  success: (data)=>
180
- viewModel.systemNotification @name, 'deleted'
181
171
  @selected null
182
172
  @items.remove(item)
183
- viewModel.loading false
184
- @onAfterDelete if @onAfterDelete?
173
+ afterDelete() if afterDelete?
174
+ @viewModel.systemNotification @name, 'deleted'
175
+ @viewModel.loading false
185
176
  @load()
186
177
  return false
187
178
 
@@ -200,10 +191,46 @@ class Db
200
191
  doDelete: (client)=>
201
192
  null
202
193
 
194
+ flashMessage: (message)=>
195
+ @viewModel.flashMessage message
196
+
197
+ errorMessage: (message)=>
198
+ @viewModel.errorMessage message
199
+
200
+ class Model
201
+ constructor: (@id, @db)->
202
+ @editing = ko.observable false
203
+ @deleting = ko.observable false
204
+
205
+ updateAttributes: (data)->
206
+ null
207
+
208
+ valid: ->
209
+ true
210
+
211
+ select: ->
212
+ @db.selected this
213
+
214
+ deselect: ->
215
+ @deleting false
216
+ @editing false
217
+ @db.selected null
218
+
219
+ edit: ->
220
+ @select()
221
+ @editing true
222
+
223
+ save: ->
224
+ return unless @valid()
225
+ @deselect()
226
+ @db.save this
227
+
228
+ doDestroy: ->
229
+ @deselect()
230
+ @db.doDestroy this
203
231
 
204
232
  window.ViewModel = ViewModel
205
- window.ClientViewModel = ClientViewModel
206
- window.AdminViewModel = AdminViewModel
207
233
  window.Db = Db
234
+ window.Model = Model
208
235
 
209
236
  window.viewModel = new ViewModel # this will probably be overridden by other pages later on
data/lib/kolo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kolo
2
- VERSION = "0.4.2"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kolo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-25 00:00:00.000000000 Z
12
+ date: 2013-06-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -93,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
93
  version: '0'
94
94
  segments:
95
95
  - 0
96
- hash: 412857391
96
+ hash: 100456755
97
97
  required_rubygems_version: !ruby/object:Gem::Requirement
98
98
  none: false
99
99
  requirements:
@@ -102,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  version: '0'
103
103
  segments:
104
104
  - 0
105
- hash: 412857391
105
+ hash: 100456755
106
106
  requirements: []
107
107
  rubyforge_project:
108
108
  rubygems_version: 1.8.24