Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
R
renderjs
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Xiaowu Zhang
renderjs
Commits
c3739581
Commit
c3739581
authored
Jun 03, 2013
by
Sven Franck
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added routerjs stateless deferred and requestService callback function
parent
56325b61
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
90 additions
and
12 deletions
+90
-12
renderjs.js
renderjs.js
+90
-12
No files found.
renderjs.js
View file @
c3739581
...
@@ -50,6 +50,10 @@
...
@@ -50,6 +50,10 @@
// validate URL:
// validate URL:
// http://bit.ly/2Ol4gj
// http://bit.ly/2Ol4gj
// deferred explanation:
// http://bit.ly/WH2TRI
// http://bit.ly/zm0Csi
/*jslint indent: 2, maxlen: 80, nomen: true */
/*jslint indent: 2, maxlen: 80, nomen: true */
/*global window: true, $: true, undefined: true, console: true,
/*global window: true, $: true, undefined: true, console: true,
document: true, require: true*/
document: true, require: true*/
...
@@ -58,7 +62,40 @@
...
@@ -58,7 +62,40 @@
var
priv
=
{},
var
priv
=
{},
that
=
{};
that
=
{};
// ================== utility methods ==================
// ================== utility methods ==================
// extend $.deferred to allow multiple calls and resolves
// thx router.js
$
.
extend
({
StatelessDeferred
:
function
()
{
var
doneList
=
$
.
Callbacks
(
"
memory
"
),
promise
=
{
done
:
doneList
.
add
,
// Get a promise for this deferred
// If obj is provided, the promise aspect is added to the object
promise
:
function
(
obj
)
{
var
i
,
keys
=
[
'
done
'
,
'
promise
'
];
if
(
obj
===
undefined
)
{
obj
=
promise
;
}
else
{
for
(
i
=
0
;
i
<
keys
.
length
;
i
+=
1
)
{
obj
[
keys
[
i
]]
=
promise
[
keys
[
i
]];
}
}
return
obj
;
}
},
deferred
=
promise
.
promise
({});
deferred
.
resolveWith
=
doneList
.
fireWith
;
deferred
.
resolve
=
doneList
.
fire
;
// All done!
return
deferred
;
}
});
// => cross-browser reduce (no support in ie8-, opera 12-)
// => cross-browser reduce (no support in ie8-, opera 12-)
// http://mzl.la/11tnDy1
// http://mzl.la/11tnDy1
...
@@ -279,6 +316,18 @@
...
@@ -279,6 +316,18 @@
// ================== internal methods ==================
// ================== internal methods ==================
// => keep service callbacks available until a postMessage returns response
priv
.
trackCallback
=
function
(
id
,
callback
,
callbackFunction
)
{
if
(
priv
.
callbackTracker
===
undefined
)
{
priv
.
callbackTracker
=
[];
}
priv
.
callbackTracker
.
push
({
"
id
"
:
id
,
"
callback
"
:
callback
,
"
callbackFunction
"
:
callbackFunction
});
};
// => keep track of service requesters to reply to
// => keep track of service requesters to reply to
priv
.
trackRequest
=
function
(
id
,
respondTo
)
{
priv
.
trackRequest
=
function
(
id
,
respondTo
)
{
if
(
priv
.
serviceTracker
===
undefined
)
{
if
(
priv
.
serviceTracker
===
undefined
)
{
...
@@ -287,6 +336,17 @@
...
@@ -287,6 +336,17 @@
priv
.
serviceTracker
.
push
({
"
id
"
:
id
,
"
respondTo
"
:
respondTo
});
priv
.
serviceTracker
.
push
({
"
id
"
:
id
,
"
respondTo
"
:
respondTo
});
};
};
// => retrieve callback for a specific service call
priv
.
retrieveCallback
=
function
(
id
)
{
var
i
,
callback
;
for
(
i
=
0
;
i
<
priv
.
callbackTracker
.
length
;
i
+=
1
)
{
callback
=
priv
.
callbackTracker
[
i
];
if
(
callback
.
id
=
id
)
{
return
[
callback
.
callback
,
callback
.
callbackFunction
];
}
}
};
// => retrieve window which called a specific service
// => retrieve window which called a specific service
priv
.
retrieveCallingWindow
=
function
(
id
)
{
priv
.
retrieveCallingWindow
=
function
(
id
)
{
var
i
,
service
,
callee
;
var
i
,
service
,
callee
;
...
@@ -456,6 +516,7 @@
...
@@ -456,6 +516,7 @@
break
;
break
;
case
"
request
"
:
case
"
request
"
:
trackingId
=
priv
.
generateUuid
();
trackingId
=
priv
.
generateUuid
();
// track this request, so we know where to send the response
// track this request, so we know where to send the response
priv
.
trackRequest
(
trackingId
,
event
.
originalTarget
);
priv
.
trackRequest
(
trackingId
,
event
.
originalTarget
);
// request the service
// request the service
...
@@ -473,23 +534,25 @@
...
@@ -473,23 +534,25 @@
priv
.
sendServiceReply
(
event
);
priv
.
sendServiceReply
(
event
);
break
;
break
;
case
"
reply
"
:
case
"
reply
"
:
priv
.
returnResult
(
event
.
data
.
result
);
priv
.
returnResult
(
event
);
break
;
break
;
}
}
};
};
// => return the result to the function call
// => return the result to the function call
priv
.
returnResult
=
function
(
resul
t
)
{
priv
.
returnResult
=
function
(
even
t
)
{
// need a way to return this result to the calling function
var
callback
=
priv
.
retrieveCallback
(
event
.
data
.
callback
);
console
.
log
(
result
);
// resolve the deferred, which includes the requestService callback
return
result
;
callback
[
0
].
resolve
(
event
.
data
.
result
,
callback
[
1
])
;
};
};
// => sends a response message after a service has been run
// => sends a response message after a service has been run
priv
.
sendServiceReply
=
function
(
event
)
{
priv
.
sendServiceReply
=
function
(
event
)
{
var
targetWindow
=
priv
.
retrieveCallingWindow
(
event
.
data
.
trackingId
);
var
targetWindow
=
priv
.
retrieveCallingWindow
(
event
.
data
.
trackingId
);
targetWindow
.
postMessage
({
targetWindow
.
postMessage
({
"
type
"
:
"
reply
"
,
"
type
"
:
"
reply
"
,
"
result
"
:
event
.
data
.
result
"
result
"
:
event
.
data
.
result
,
"
callback
"
:
event
.
data
.
callbackId
,
},
window
.
location
.
href
.
split
(
"
?
"
)[
0
]);
},
window
.
location
.
href
.
split
(
"
?
"
)[
0
]);
};
};
...
@@ -499,7 +562,8 @@
...
@@ -499,7 +562,8 @@
window
.
top
.
postMessage
({
window
.
top
.
postMessage
({
"
type
"
:
"
result
"
,
"
type
"
:
"
result
"
,
"
result
"
:
result
,
"
result
"
:
result
,
"
trackingId
"
:
event
.
data
.
trackingId
"
trackingId
"
:
event
.
data
.
trackingId
,
"
callbackId
"
:
event
.
data
.
callbackId
,
},
window
.
location
.
href
.
split
(
"
?
"
)[
0
]);
},
window
.
location
.
href
.
split
(
"
?
"
)[
0
]);
};
};
...
@@ -535,10 +599,12 @@
...
@@ -535,10 +599,12 @@
return
tgt
&&
tgt
.
getElementById
(
o
).
contentWindow
;
return
tgt
&&
tgt
.
getElementById
(
o
).
contentWindow
;
},
document
);
},
document
);
}
}
// and request the service
// and request the service
targetWindow
.
postMessage
({
targetWindow
.
postMessage
({
"
type
"
:
"
run
"
,
"
type
"
:
"
run
"
,
"
trackingId
"
:
trackingId
,
"
trackingId
"
:
trackingId
,
"
callbackId
"
:
event
.
data
.
callbackId
,
"
service
"
:
event
.
data
.
service
,
"
service
"
:
event
.
data
.
service
,
"
parameters
"
:
event
.
data
.
parameters
"
parameters
"
:
event
.
data
.
parameters
},
window
.
location
.
href
);
},
window
.
location
.
href
);
...
@@ -826,11 +892,25 @@
...
@@ -826,11 +892,25 @@
};
};
// => request a service to be run
// => request a service to be run
that
.
requestService
=
$
.
fn
.
requestService
=
function
(
options
)
{
that
.
requestService
=
$
.
fn
.
requestService
=
function
(
options
,
callbackFunction
)
{
var
deferred
=
new
$
.
StatelessDeferred
(),
callbackId
=
priv
.
generateUuid
(),
callback
=
deferred
;
// store callback to be retrieved by response handler
priv
.
trackCallback
(
callbackId
,
callback
,
callbackFunction
);
// set type
// set type
if
(
options
.
type
===
undefined
)
{
if
(
options
.
type
===
undefined
)
{
options
.
type
=
"
request/any
"
;
options
.
type
=
"
request/any
"
;
}
}
deferred
.
done
(
function
(
result
,
callbackFunction
)
{
if
(
callbackFunction
)
{
callbackFunction
(
result
);
}
});
options
.
callbackId
=
callbackId
;
window
.
top
.
postMessage
(
options
,
window
.
location
.
href
);
window
.
top
.
postMessage
(
options
,
window
.
location
.
href
);
};
};
...
@@ -887,10 +967,8 @@
...
@@ -887,10 +967,8 @@
success
:
function
(
data
)
{
success
:
function
(
data
)
{
priv
.
appendGadget
(
data
,
options
);
priv
.
appendGadget
(
data
,
options
);
},
},
error
:
function
(
error
,
status
,
message
)
{
error
:
function
(
error
)
{
console
.
log
(
error
);
console
.
log
(
error
);
console
.
log
(
status
);
console
.
log
(
message
);
}
}
});
});
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment