Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 7a5865a

Browse files
committed
view fixes
1 parent 09dc475 commit 7a5865a

File tree

5 files changed

+106
-140
lines changed

5 files changed

+106
-140
lines changed

src/chaplin/views/collection_view.coffee

Lines changed: 36 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,89 +5,57 @@ Backbone = require 'backbone'
55
View = require 'chaplin/views/view'
66
utils = require 'chaplin/lib/utils'
77

8-
# Shortcut to access the DOM manipulation library.
9-
$ = Backbone.$
10-
118
filterChildren = (nodeList, selector) ->
129
return nodeList unless selector
1310
for node in nodeList when Backbone.utils.matchesSelector node, selector
1411
node
1512

16-
toggleElement = do ->
17-
if $
18-
(elem, visible) -> $(elem).toggle visible
13+
toggleElement = (elem, visible) ->
14+
if Backbone.$
15+
$(elem).toggle visible
1916
else
20-
(elem, visible) ->
21-
elem.style.display = (if visible then '' else 'none')
17+
elem.style.display = (if visible then '' else 'none')
2218

2319
startAnimation = (elem, useCssAnimation, cls) ->
2420
if useCssAnimation
2521
elem.classList.add cls
2622
else
2723
elem.style.opacity = 0
2824

29-
endAnimation = do ->
30-
if $
31-
(elem, duration) -> elem.animate {opacity: 1}, duration
32-
else
33-
(elem, duration) ->
34-
elem.style.transition = "opacity #{(duration / 1000)}s"
35-
elem.opacity = 1
36-
37-
insertView = do ->
38-
if $
39-
(list, viewEl, position, length, itemSelector) ->
40-
$list = $ list
41-
insertInMiddle = (0 < position < length)
42-
isEnd = (length) -> length is 0 or position is length
43-
44-
if insertInMiddle or itemSelector
45-
# Get the children which originate from item views.
46-
children = $list.children itemSelector
47-
childrenLength = children.length
48-
49-
# Check if it needs to be inserted.
50-
unless children[position] is viewEl
51-
if isEnd childrenLength
52-
# Insert at the end.
53-
$list.append viewEl
54-
else
55-
# Insert at the right position.
56-
if position is 0
57-
children.eq(position).before viewEl
58-
else
59-
children.eq(position - 1).after viewEl
60-
else
61-
method = if isEnd length then 'append' else 'prepend'
62-
$list[method] viewEl
25+
endAnimation = (elem, duration) ->
26+
if Backbone.$
27+
elem.animate {opacity: 1}, duration
6328
else
64-
(list, viewEl, position, length, itemSelector) ->
65-
insertInMiddle = (0 < position < length)
66-
isEnd = (length) -> length is 0 or position is length
67-
68-
if insertInMiddle or itemSelector
69-
# Get the children which originate from item views.
70-
children = filterChildren list.children, itemSelector
71-
childrenLength = children.length
72-
73-
# Check if it needs to be inserted.
74-
unless children[position] is viewEl
75-
if isEnd childrenLength
76-
# Insert at the end.
77-
list.appendChild viewEl
78-
else if position is 0
79-
# Insert at the right position.
80-
list.insertBefore viewEl, children[position]
81-
else
82-
last = children[position - 1]
83-
if list.lastChild is last
84-
list.appendChild viewEl
85-
else
86-
list.insertBefore viewEl, last.nextElementSibling
87-
else if isEnd length
29+
elem.style.transition = "opacity #{(duration / 1000)}s"
30+
elem.opacity = 1
31+
32+
insertView = (list, viewEl, position, length, itemSelector) ->
33+
insertInMiddle = (0 < position < length)
34+
isEnd = (length) -> length is 0 or position is length
35+
36+
if insertInMiddle or itemSelector
37+
# Get the children which originate from item views.
38+
children = filterChildren list.children, itemSelector
39+
childrenLength = children.length
40+
41+
# Check if it needs to be inserted.
42+
unless children[position] is viewEl
43+
if isEnd childrenLength
44+
# Insert at the end.
8845
list.appendChild viewEl
46+
else if position is 0
47+
# Insert at the right position.
48+
list.insertBefore viewEl, children[position]
8949
else
90-
list.insertBefore viewEl, list.firstChild
50+
last = children[position - 1]
51+
if list.lastChild is last
52+
list.appendChild viewEl
53+
else
54+
list.insertBefore viewEl, last.nextElementSibling
55+
else if isEnd length
56+
list.appendChild viewEl
57+
else
58+
list.insertBefore viewEl, list.firstChild
9159

9260
# General class for rendering Collections.
9361
# Derive this class and declare at least `itemView` or override
@@ -163,7 +131,7 @@ module.exports = class CollectionView extends View
163131
# A function that will be executed after each filter.
164132
# Hides excluded items by default.
165133
filterCallback: (view, included) ->
166-
view.$el.stop(true, true) if $
134+
view.$el.stop(true, true) if Backbone.$
167135
toggleElement view.el, included
168136

169137
# View lists

src/chaplin/views/layout.coffee

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ utils = require 'chaplin/lib/utils'
77
EventBroker = require 'chaplin/lib/event_broker'
88
View = require 'chaplin/views/view'
99

10-
# Shortcut to access the DOM manipulation library.
11-
$ = Backbone.$
12-
1310
module.exports = class Layout extends View
1411
# Bind to document body by default.
1512
el: 'body'
@@ -100,7 +97,7 @@ module.exports = class Layout extends View
10097
openLink: (event) =>
10198
return if utils.modifierKeyPressed(event)
10299

103-
el = if $ then event.currentTarget else event.delegateTarget
100+
el = if Backbone.$ then event.currentTarget else event.delegateTarget
104101
isAnchor = el.nodeName is 'A'
105102

106103
# Get the href and perform checks on it.
@@ -118,7 +115,7 @@ module.exports = class Layout extends View
118115
skipRouting = @settings.skipRouting
119116
type = typeof skipRouting
120117
return if type is 'function' and not skipRouting(href, el) or
121-
type is 'string' and (if $ then $(el).is(skipRouting) else Backbone.utils.matchesSelector el, skipRouting)
118+
type is 'string' and (if Backbone.$ then Backbone.$(el).is(skipRouting) else el.matches skipRouting)
122119

123120
# Handle external links.
124121
external = isAnchor and @isExternalLink el
@@ -206,14 +203,14 @@ module.exports = class Layout extends View
206203

207204
# Apply the region selector.
208205
instance.container = if region.selector is ''
209-
if $
206+
if Backbone.$
210207
region.instance.$el
211208
else
212209
region.instance.el
213210
else
214211
if region.instance.noWrap
215-
if $
216-
$(region.instance.container).find region.selector
212+
if Backbone.$
213+
Backbone.$(region.instance.container).find region.selector
217214
else
218215
region.instance.container.querySelector region.selector
219216
else

src/chaplin/views/view.coffee

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,6 @@ mediator = require 'chaplin/mediator'
66
EventBroker = require 'chaplin/lib/event_broker'
77
utils = require 'chaplin/lib/utils'
88

9-
# Shortcut to access the DOM manipulation library.
10-
$ = Backbone.$
11-
12-
# Function bind shortcut.
13-
bind = do ->
14-
if Function::bind
15-
(item, ctx) -> item.bind ctx
16-
else if _.bind
17-
_.bind
18-
19-
setHTML = do ->
20-
if $
21-
(elem, html) -> $(elem).html html
22-
else
23-
(elem, html) -> elem.innerHTML = html
24-
25-
attach = do ->
26-
if $
27-
(view) ->
28-
actual = $(view.container)
29-
if typeof view.containerMethod is 'function'
30-
view.containerMethod actual, view.el
31-
else
32-
actual[view.containerMethod] view.el
33-
else
34-
(view) ->
35-
actual = if typeof view.container is 'string'
36-
document.querySelector view.container
37-
else
38-
view.container
39-
40-
if typeof view.containerMethod is 'function'
41-
view.containerMethod actual, view.el
42-
else
43-
actual[view.containerMethod] view.el
44-
459
module.exports = class View extends Backbone.View
4610
# Mixin an EventBroker.
4711
_.extend @prototype, EventBroker
@@ -69,7 +33,7 @@ module.exports = class View extends Backbone.View
6933

7034
# Method which is used for adding the view to the DOM
7135
# Like jQuery’s `html`, `prepend`, `append`, `after`, `before` etc.
72-
containerMethod: if $ then 'append' else 'appendChild'
36+
containerMethod: if Backbone.$ then 'append' else 'appendChild'
7337

7438
# Regions
7539
# -------
@@ -152,7 +116,7 @@ module.exports = class View extends Backbone.View
152116
@el =
153117
if region.instance.container?
154118
if region.instance.region?
155-
$(region.instance.container).find region.selector
119+
Backbone.$(region.instance.container).find region.selector
156120
else
157121
region.instance.container
158122
else
@@ -193,7 +157,7 @@ module.exports = class View extends Backbone.View
193157
match = key.match /^(\S+)\s*(.*)$/
194158
eventName = "#{match[1]}.delegateEvents#{@cid}"
195159
selector = match[2]
196-
bound = bind handler, this
160+
bound = _.bind handler, this
197161
@delegate eventName, (selector or null), bound
198162
return
199163

@@ -367,20 +331,42 @@ module.exports = class View extends Backbone.View
367331
# Delegate events to the top-level container in the template.
368332
@setElement el.firstChild, true
369333
else
370-
setHTML @el, html
334+
@setHTML html
371335

372336
# Return the view.
373337
this
374338

339+
# Set the innerHTML of the view's `el`. Override this method to support
340+
# older browsers (IE needs the html empty before, e.g.)
341+
setHTML: (html) ->
342+
@el.innerHTML = html
343+
375344
# This method is called after a specific `render` of a derived class.
376345
attach: ->
377346
# Attempt to bind this view to its named region.
378347
mediator.execute 'region:show', @region, this if @region?
379348

380349
# Automatically append to DOM if the container element is set.
381350
if @container and not document.body.contains @el
382-
attach this
383-
# Trigger an event.
351+
if Backbone.$
352+
actual = Backbone.$(this.container)
353+
if typeof this.containerMethod is 'function'
354+
this.containerMethod actual, this.el
355+
else
356+
actual[this.containerMethod] this.el
357+
else
358+
actual = if typeof this.container is 'string'
359+
document.querySelector this.container
360+
else
361+
this.container
362+
363+
if typeof this.containerMethod is 'function'
364+
this.containerMethod actual, this.el
365+
else
366+
actual[this.containerMethod] this.el
367+
368+
# Hook into this event for things that require the view to be in the DOM
369+
# (like calculating height / width or position).
384370
@trigger 'addedToDOM'
385371

386372
# Disposal

test/initialize.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@ window.clearInterval = timers.clearInterval;
99
var paths = {};
1010
var componentsFolder = 'bower_components';
1111

12-
var match = window.location.search.match(/type=([-\w]+)/);
12+
var match = window.location.search.match(/type=([-\w]+)&useDeps=([-\w]+)/);
1313
var testType = window.testType || (match ? match[1] : 'backbone');
14+
var useDeps = window.useDeps || (match ? match[2] : true);
1415

1516
var addDeps = function() {
16-
paths.underscore = '../' + componentsFolder + '/lodash/lodash.compat';
17-
paths.jquery = '../' + componentsFolder + '/jquery/jquery';
17+
if (useDeps) {
18+
paths.underscore = '../' + componentsFolder + '/lodash/lodash.compat';
19+
paths.jquery = '../' + componentsFolder + '/jquery/jquery';
20+
} else {
21+
paths.NativeView = '../' + componentsFolder + '/Backbone.NativeView/backbone.nativeview';
22+
}
1823
};
1924
if (testType === 'backbone') {
20-
paths.backbone = '../' + componentsFolder + '/backbone/backbone'
21-
addDeps()
25+
paths.backbone = '../' + componentsFolder + '/backbone/backbone';
26+
addDeps();
2227
} else {
23-
if (testType === 'deps') addDeps();
24-
paths.backbone = '../' + componentsFolder + '/exoskeleton/exoskeleton'
28+
addDeps();
29+
paths.backbone = '../' + componentsFolder + '/exoskeleton/exoskeleton';
2530
}
2631

2732
var config = {
@@ -46,7 +51,11 @@ if (testType === 'backbone' || testType === 'deps') {
4651
requirejs.config(config);
4752
if (testType === 'exos') {
4853
define('jquery', function(){});
49-
define('underscore', ['backbone'], function(Backbone){return Backbone.utils;});
54+
define('underscore', ['backbone'], function(Backbone){
55+
var _ = Backbone.utils;
56+
_.bind = function(fn, ctx) { return fn.bind(ctx); }
57+
return _;
58+
});
5059
}
5160
mocha.setup({ui: 'bdd', ignoreLeaks: true});
5261
// Wonderful hack to send a message to grunt from inside a mocha test.
@@ -86,16 +95,26 @@ window.addEventListener('DOMContentLoaded', function() {
8695
'view',
8796
'utils',
8897
'sync_machine'
89-
];
90-
var loaded = [];
91-
for (var i = 0, l = specs.length; i < l; i++) {
92-
loaded.push(specs[i] + '_spec');
93-
}
94-
require(loaded, function() {
98+
].map(function(file) {
99+
return file + '_spec';
100+
});
101+
102+
var run = function() {
95103
if (window.mochaPhantomJS) {
96104
mochaPhantomJS.run();
97105
} else {
98106
mocha.run();
99107
}
108+
};
109+
110+
require(specs, function() {
111+
if (useDeps) {
112+
run();
113+
} else {
114+
require(['backbone', 'NativeView'], function(Backbone, NativeView) {
115+
Backbone.View = NativeView;
116+
run();
117+
});
118+
}
100119
});
101120
}, false);

0 commit comments

Comments
 (0)