forked from jupiterjs/jquerymx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.js
More file actions
110 lines (100 loc) · 3.69 KB
/
view.js
File metadata and controls
110 lines (100 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
steal.plugins('jquery/controller', 'jquery/view').then(function( $ ) {
jQuery.Controller.getFolder = function() {
return jQuery.String.underscore(this.fullName.replace(/\./g, "/")).replace("/Controllers", "");
};
var calculatePosition = function( Class, view, action_name ) {
var slashes = Class.fullName.replace(/\./g, "/"),
hasControllers = slashes.indexOf("/Controllers/" + Class.shortName) != -1,
path = jQuery.String.underscore(slashes.replace("/Controllers/" + Class.shortName, "")),
controller_name = Class._shortName,
suffix = (typeof view == "string" && view.match(/\.[\w\d]+$/)) || jQuery.View.ext;
//calculate view
if ( typeof view == "string" ) {
if ( view.substr(0, 2) == "//" ) { //leave where it is
} else {
view = "//" + new steal.File('views/' + (view.indexOf('/') !== -1 ? view : (hasControllers ? controller_name + '/' : "") + view)).joinFrom(path) + suffix;
}
} else if (!view ) {
view = "//" + new steal.File('views/' + (hasControllers ? controller_name + '/' : "") + action_name.replace(/\.|#/g, '').replace(/ /g, '_')).joinFrom(path) + suffix;
}
return view;
};
var calculateHelpers = function( myhelpers ) {
var helpers = {};
if ( myhelpers ) {
if ( jQuery.isArray(myhelpers) ) {
for ( var h = 0; h < myhelpers.length; h++ ) {
jQuery.extend(helpers, myhelpers[h]);
}
}
else {
jQuery.extend(helpers, myhelpers);
}
} else {
if ( this._default_helpers ) {
helpers = this._default_helpers;
}
//load from name
var current = window;
var parts = this.Class.fullName.split(/\./);
for ( var i = 0; i < parts.length; i++ ) {
if ( typeof current.Helpers == 'object' ) {
jQuery.extend(helpers, current.Helpers);
}
current = current[parts[i]];
}
if ( typeof current.Helpers == 'object' ) {
jQuery.extend(helpers, current.Helpers);
}
this._default_helpers = helpers;
}
return helpers;
};
/**
* @add jQuery.Controller.prototype
*/
jQuery.Controller.prototype.
/**
* @tag view
* Renders a View template with the controller instance. If the first argument
* is not supplied,
* it looks for a view in /views/controller_name/action_name.ejs.
* If data is not provided, it uses the controller instance as data.
* @codestart
* TasksController = $.Controller.extend('TasksController',{
* click: function( el ) {
* // renders with views/tasks/click.ejs
* el.html( this.view() )
* // renders with views/tasks/under.ejs
* el.after( this.view("under", [1,2]) );
* // renders with views/shared/top.ejs
* el.before( this.view("shared/top", {phrase: "hi"}) );
* }
* })
* @codeend
* @plugin controller/view
* @return {String} the rendered result of the view.
* @param {String} [optional1] view The view you are going to render. If a view isn't explicity given
* this function will try to guess at the correct view as show in the example code above.
* @param {Object} [optional2] data data to be provided to the view. If not present, the controller instance
* is used.
* @param {Object} [optional3] myhelpers an object of helpers that will be available in the view. If not present
* this controller class's "Helpers" property will be used.
*
*/
view = function( view, data, myhelpers ) {
//shift args if no view is provided
if ( typeof view != "string" && !myhelpers ) {
myhelpers = data;
data = view;
view = null;
}
//guess from controller name
view = calculatePosition(this.Class, view, this.called);
//calculate data
data = data || this;
//calculate helpers
var helpers = calculateHelpers.call(this, myhelpers);
return jQuery.View(view, data, helpers); //what about controllers in other folders?
};
});