public
Description: SPAkit is a plugin that makes it very easy to turn your Rails App Into a "single page application" (SPA).
Homepage: http://handlino.com/blog/2008/02/17/52/
Clone URL: git://github.com/ihower/spakit.git
initial import
Wen-Tien Chang (author)
Fri Apr 04 00:18:29 -0700 2008
commit  bcc0c3bcdbca4e7c0e18f76e305dde731ffb4b86
tree    825503befbf61ab21e583ad5a85cdf33ebe2e43c
parent  2aec2f444093983d2db49e312ad4526f0f7fee01
0
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
0
@@ -0,0 +1,134 @@
0
+Author:: Wen-Tien Chang(mailto:ihower@gmail.com), hlb(mailto:hlb@handlino.com)
0
+Copyright:: Copyright (c) 2007 Handlino Inc.
0
+License:: Distributed under the New BSD License
0
+
0
+== Description ==
0
+Spakit is a plugin that makes it very easy to turn your Rails App
0
+Into a "single page application" (SPA).
0
+
0
+To enable SPA, simply install the plug-in, create a spakit layout and use Spakit helper.
0
+Don't need modify any model/controller code... ;)
0
+
0
+Spakit will send xhr request(i.e. prototype's Ajax.Updater ) with ?layout=spakit,
0
+and controller return HTML with spakit layout. Note that spakit_form_for not support file upload submit.
0
+
0
+By the way, Message plugin is another SPA plugin,using different approach.
0
+
0
+== Usage ==
0
+
0
+For now, Spakit provide three helper:
0
+
0
+ * spakit_link_to, just like remote_link_to
0
+ * spakit_form_for, just like form_for, but you must specify :url
0
+ * spakit_form_tag, just like form_tag
0
+
0
+by default spakit will replace HTML element called #content.
0
+
0
+== Layout code example ==
0
+You should write flash message here because we often place that in application layout.
0
+
0
+# /view/layouts/spakit.rhtml
0
+
0
+<p><%= flash[:notice] %></p>
0
+<%= yield %>
0
+
0
+== History Bookmarks ==
0
+
0
+We recommend Really Simple History(RSH) library to handle browser forward/back.
0
+
0
+Example code:
0
+
0
+# environment.rb
0
+
0
+module SpakitHelper
0
+ @@spa_options = {
0
+ :update => 'content-region',
0
+ :loading => 'SPA.loading',
0
+ :complete => 'SPA.complete'
0
+ }
0
+end
0
+
0
+# application.js
0
+# We use jquery syntax, you can use prototype to define your function.
0
+
0
+var $j = jQuery.noConflict();
0
+
0
+(function($) {
0
+ SPA = {
0
+ currentHash: null,
0
+ currentLocation: null,
0
+ init: function() {
0
+ if ( $('#waiting-message').length == 0 ) {
0
+ $('<div id="waiting-message"><img src="/images/ajax-loader.gif"></div>').appendTo('#bd');
0
+ $('#waiting-message').hide();
0
+ }
0
+ },
0
+ loading: function() {
0
+ this.init();
0
+ $('#waiting-message').show();
0
+ },
0
+ hide: function() {
0
+ $('#waiting-message').hide();
0
+ },
0
+ historyChange: function(newLocation, historyData, isFresh) {
0
+ if (isFresh == null) {
0
+ if (historyStorage.hasKey(newLocation)) {
0
+ if (newLocation == "start") {
0
+ newLocation = location.pathname;
0
+ }
0
+
0
+ $.ajax({
0
+ type: "GET",
0
+ url: newLocation,
0
+ data: { layout: "spakit" },
0
+ beforeSend: function(){ SPA.loading(); },
0
+ complete: function(res, status){
0
+ if ( status == "success" || status == "notmodified" ) {
0
+ $('#content-region').html(res.responseText);
0
+ }
0
+ SPA.hide();
0
+ }
0
+ });
0
+ }
0
+ } else {
0
+ /* new data */
0
+ dhtmlHistory.add(newLocation, historyData);
0
+ }
0
+ },
0
+ complete: function(newLocation) {
0
+ this.historyChange(newLocation, "", true);
0
+ this.hide();
0
+ }
0
+ };
0
+
0
+ SPA.currentHash = window.location.hash;
0
+ if (SPA.currentHash.length) {
0
+ if (SPA.currentHash.charAt(0) == '#' && SPA.currentHash.charAt(1) == '/') {
0
+ SPA.currentLocation = SPA.currentHash.slice(1);
0
+ }
0
+ }
0
+
0
+ $(document).ready(function(){
0
+ dhtmlHistory.initialize();
0
+ if (SPA.currentLocation) {
0
+ if (SPA.currentLocation != '#start') {
0
+ window.location.href = SPA.currentLocation;
0
+ }
0
+ } else {
0
+ dhtmlHistory.addListener(SPA.historyChange);
0
+ if (dhtmlHistory.isFirstLoad()) {
0
+ dhtmlHistory.add("start", "");
0
+ }
0
+ }
0
+ });
0
+
0
+})(jQuery);
0
+
0
+window.dhtmlHistory.create({
0
+ toJSON: function(o) {
0
+ return JSON.stringify(o);
0
+ },
0
+ fromJSON: function(s) {
0
+ return JSON.parse(s);
0
+ }
0
+});
0
\ No newline at end of file

Comments

    No one has commented yet.