mikehostetler / amplify Goto Github PK
View Code? Open in Web Editor NEWAmplifyJS
Home Page: http://amplifyjs.com
License: GNU General Public License v2.0
AmplifyJS
Home Page: http://amplifyjs.com
License: GNU General Public License v2.0
I am running IE 7.0.5730.13 running in a VMware image that Microsoft makes available for testing purposes and it fails everytime.
You can download the images at:
http://www.microsoft.com/downloads/en/details.aspx?FamilyId=21EABB90-958F-4B64-B5F1-73D0A413C8EF&displaylang=en
(they are VPC image files but it is possible to make them into VMWare Images)
I am using this link to test in fiddle:
http://jsfiddle.net/gh/get/jquery/1.5/appendto/amplify/tree/master/store/demo/implicit/
I get this javascript error:
Line:204
Char:6
Error: 'JSON' is undefined
thanks,
Carlos
I would like to be able to use the following code:
amplify.subscribe( "message.save message.update message.new", function () {
// Do something whenever a message is saved, updated, or created
});
Right now, it would have to be written like this:
var callback = function () {
// Do something whenever a message is saved, updated, or created
});
amplify.subscribe( "message.save", callback );
amplify.subscribe( "message.update", callback );
amplify.subscribe( "message.new", callback );
When a request is completed, a callback to a "complete" function should be made as well as the current publish "request.complete".
Ever since jQuery officially supports wrapping non-DOM-elements, it essentially contains a generic pub/sub system:
$(anyobject).bind("dataexample", function(e, data) { alert(data.foo); });
...
$(anyobject).triggerHandler("dataexample", { foo: "bar" });
So why not just use that instead of the custom pub/sub implementation in core
?
amplify.request()
should return jQuery.Deferred()
object.
Example:
$.when(amplify.request("resource1"),
amplify.request("resource2"))
.then(function(data1, data2) {
console.log("Resources are loaded!");
},
function() {
console.log("Requests failed!");
}
)```
The documentation for persistent cache in amplify.request says that you get persistent caching if you include amplify.store, but we don't state that amplify.store must be included first.
I'd like to have access to the invoked request/resource in a decoder. As far as I can tell this is currently not supported, but should be easy to implement. Passing request, settings and ajaxSettings to decoder() probably.
Any chance for this happening?
http://amplifyjs.com/api/request/#custom_cache
Needs:
With*Storage
and userData
, if I replace a previously-expirable value and don't set an expiration, there is no expiration (which is the behavior I expect). With memory
storage however, the first expiration will delete the replaced value (the setTimeout
is not cleared).
amplify.store[type]( 'key', 'value1', {expires: 500} );
amplify.store[type]( 'key', 'value2' );
window.setTimeout( function(){ alert( amplify.store[type]( 'key' ) ) }, 1000 );
This JsFiddle tests all storage types available to the current browser.
When multiple concurrent requests occur, it would be valuable for amplify.js to reduce these requests to one ajax call.
For example, given:
amplify.request.define("X", "ajax", {
url: "/x/{id}",
dataType: "json",
cache: true,
type: "GET"
});
amplify.request("X", { id: 1}, function () {});
amplify.request("X", { id: 1}, function () {});
Right now when I run this code (e.g. http://jsfiddle.net/Xg9gn/) two AJAX requests occur.
Obviously caching cannot occur until a request completes.
However, I believe a preferable model would be for the first request to block all future identical requests; those future requests would however subscribe to the results of the first request.
An option to force an AJAX request may be prudent (though I cannot think of a use case offhand).
Just a thought! :)
This is a simple change, which would be as follows:
amplify.request.decoders = {
jsend: function( data, status, ampXHR, success, error ) {
var handled = false; // ADDED
if ( data.status === "success" ) {
success( data.data );
handled = true; // ADDED
} else if ( data.status === "fail" ) {
error( data.data, "fail" );
handled = true; // ADDED
} else if ( data.status === "error" ) {
delete data.status;
error( data, "error" );
handled = true; // ADDED
}
if (!handled) {
throw "JSON response does not conform to jsend spec";
} // ADDED
}
}
This can help track down nasty oversights, especially with spelling errors e.g. "suceess".
Because jQuery uses its own expando property to establish a unique global for the JSONP callback, amplify's caching mechanism is broken. It works again if you override using jQuery's jsonpCallback property. Perhaps a warning or amplify should explicitly set its own jsonpCallback?
I was originally going to say "better documentation for timeout / cache parameters", but I really feel like this is a product-wide issue.
Going to the "Store" method's documentation (http://amplifyjs.com/api/store/), the entry for "expires" is
expires: Duration in milliseconds that the value should be cached
That's it!
There also seems to be no information on how to clear the entire cache or what happens when you do explicit storage usage and the browser doesn't support it.
I know there are forums where this information may be found, but someone should be updating the documentation with this information so we don't have the "needle in the haystack" approach to finding documentation (searching through forum posts)
On server 2008 r2 running chrome/firefox sessionStorage functions fine, however IE9 on the same machine will throw:
Object doesn't support property or method 'sessionStorage'
This line, in the block to support IE 5+, throws an exception
if ( div.addBehavior ) {
div.addBehavior( "#default#userdata" ); //throws "unspecified error" in IE6 SP2
I've verified this on several of our QA lab boxes with IE6 SP2. The versions affected (so far): 6.0.3790.3959 SP2
It would be cool to use amplify as an module without globals that can be "required" with AMD. JQuery, Knockout etc. also support such scenarios and the implementation is not difficult.
See https://github.com/umdjs/umd/ for implementation tips...
Now that jQuery is exposing itself w/ define, I feel like this will catch on more.
The easiest implementation would be:
define('amplify', function()
{
return amplify;
});
But the most correct would be to not use a global variable at all if an AMD loader exists on the page (underscore.js does a good job of this, I believe).
This allows amplify to be loaded using RequireJS or equivalent.
When I make an amplify.js request
, and the response does not conform to the jsend spec, it would be nice if amplify.js threw an exception (or could be run in "strict" mode where it would throw an exception).
This would help a little bit with debugging, since at the moment it seems that when an amplify.js request
does not conform to the jsend spec neither the success nor error handler are called - the response is silently ignored. Explicit failure is preferable.
Thanks!
Here's a testcase: http://jsbin.com/3/oxexoc
On initial page load, everything works fine until you click on a web page (on another domain) then navigate Back. The localStorage
reference in amplify.store code seems lost and returns a zero length
, which probably explains why amplify.store('key') is always returning null
. If you try to access directly to the localStorage
instance, the key is actually there.
This bug is replicable on Safari 5.1.2 (7534.52.7) on Windows and Mobile Safari (7534.48.3) on iOS5 (iPhone).
I was on page: http://amplifyjs.com/api/store/ and tried to run the fiddler sample and i get the following error message(s).
Screenshot: http://screencast.com/t/qRK5dkk28Lo
Console output:
SEC7112: Script from https://raw.github.com/appendto/amplify/master/core/amplify.core.js was blocked due to mime type mismatch
light
SEC7112: Script from https://raw.github.com/appendto/amplify/master/store/amplify.store.js was blocked due to mime type mismatch
light
SCRIPT5009: 'amplify' is undefined
light, line 34 character 5
For security reasons, a UserData store is available only in the same directory and with the same protocol used to persist the store.
At line 201 in amplify.store.js, the regex used to sanitize the key is incorrect.
Unicode character \u37f is incorrect an should be corrected as \u037f
key = key.replace( /[^-._0-9A-Za-z\xb7\xc0-\xd6\xd8-\xf6\xf8-\u037d\u37f-\u1fff\u200c-\u200d\u203f\u2040\u2070-\u218f]/g, "-" );
should be
key = key.replace( /[^-._0-9A-Za-z\xb7\xc0-\xd6\xd8-\xf6\xf8-\u037d\u037f-\u1fff\u200c-\u200d\u203f\u2040\u2070-\u218f]/g, "-" );
This regex cause some parsing errors, such in IDE Visual Studio or with bundling feature in ASP.NET MVC4.
I've been checking also the /amplify/src/store/test/test.html unit-test and it won't load the page at all under IE6 or 7
Amplify.store starts "losing" data when it is in use on more than one browser tab for the same website. It seems that whichever tab/window writes last keeps its data, but the unlucky tab that didn't write last loses its data.
Next milestone for AmplifyJS release
The min version of amplified must have ';' at the end of the script.
i.e. (function(a,b){......}(amplify,jQuery);
While combining multiple js files, error is raised in subsequent script module.
I suggest to modify the build to add colon at the end as well as at the start of the script. At the start of the script will help reduce any other script's similar issue.
In the request documentation, there is Any settings found in jQuery.ajax().
However, doesn't appear to support xhrFields which is needed for cross domain authentication, see here
Does amplify request support cross-domain requests with authentication?
Hi,
I could be doing something ridiculously dumb, but I am trying to use amplify.request() to retrieve a user's github repos. Every example I read, the "data" parameter of the success callback is what it should be: the JSON deserialized into an object. However when I try and do this, the data parameter is actually an object with two properties, data.meta
and data.data
, with the data.data
containing what I actually want.
Am I doing something wrong? Below is my source code.
$(function(){
amplify.request.define("github-repos", "ajax", {
url: "https://api.github.com/users/cocoahero/repos",
dataType: "jsonp",
type: "GET"
});
amplify.request("github-repos", function(data) {
var html = "";
$.each(data.data, function(i, repo) {
html += "<h2>" + repo.name + "</h2>" + "<p>" + repo.description + "</p>";
});
$("#repos").html(html);
});
});
Thanks!
Hi,
If I try and unsubscribe my callback during the callback itself I get
the following error (in Firefox 6 - but I think browser is
irrelevant):
subscription is undefined
[Break On This Error] ret =
subscription.callback.apply( subscription.context, args );
amplify.js (line 38)
which is the 3rd line in this snippet:
for ( length = subscriptions[ topic ].length; i < length; i++ ) {
subscription = subscriptions[ topic ][ i ];
>>>> ret = subscription.callback.apply( subscription.context, args );
if ( ret === false ) {
break;
}
}
I think this is happening because my call to unsubscribe is removing a
callback from subscriptions[ topic ], but the loop does not expect
this. Sample code as follows:
function oneTimeOnlyCallback(data) {
amplify.unsubscribe(topic, oneTimeOnlyCallback);
doSomething(data);
}
amplify.subscribe(topic, oneTimeOnlyCallback);
My question is, is there any way to have a callback unsubscribe itself
from a topic during it receiving the notification from publish?
Without it breaking this loop.
I want to do this as I only want to be notified of a particular topic
once, and don't want the subscription for the topic left registered
after this.
Cheers.
Paul.
IE 9 appears to have unlimited localStorage and sessionStorage support when using direct access via dot or bracket notation. However, there is a very real limit of 5 MB when using .setItem(). Figure out whether there really is unlimited storage with direct access and switch to using .setItem() for proper error handling if not (this may be a good idea regardless). Also, check if userData is really unlimited.
We shouldn't encourage users to explicitly use anything other than sessionStorage. If the user wants the data to persist indefinitely, they should use the default store.
"Use of globalStorage is deprecated. Please use localStorage instead."
IMHO the code should check if localStorage can be used, and if so, don't bother checking for globalStorage.
As of AmplifyJS v1.1.0 there is no check performed about if given callback is already subscribed to the topic. It leads to a problem of several calls to same callback upon message publishing.
Possible way to avoid it is to add some kind of checking of callbacks uniqueness e.g. by using some kind of hash function for them.
I ran into a situation where my subscriptions weren't being called on publish, and through some digging discovered it would happen in the following situation:
// This handler will always work when topic1 or topic2 are published
amplify.subscribe( "topic1 topic2", function () { ... });
....
// Only when topic1 is fired will this ever work, topic3 is ignored.
amplify.subscribe( "topic1 topic3", function () { ... });
If you start a multiple topic subscription with a topic that has already been subscribed once, the remaining topics are ignored. If you change up the test in core/test/unit.js
, you can see the error:
test( "multiple subscriptions", function() {
expect( 4 );
amplify.subscribe( "sub-a-1 sub-a-2 sub-a-3", function() {
ok( true );
});
amplify.publish( "sub-a-1" );
amplify.subscribe( "sub-b-1 sub-b-2", function() {
ok( true );
});
// All topics but the first one are ignored if the first
// topic in the string has already been subscribed.
amplify.subscribe( "sub-b-1 sub-b-3", function() {
ok( true );
});
amplify.publish( "sub-b-2" );
amplify.publish( "sub-b-2" );
amplify.publish( "sub-b-3" );
});
Sometimes I'd like to handle a number of similar topics with the same function. I'm imagining something like this:
amplify.subscribe(["foo", "bar", "baz"], function(data, topic) {
alert("got a " + topic + " with " + data);
});
For that matter, a handy feature for debugging and logging would be subscribeAll().
amplify.request.define('custom', 'ajax', {
url: '/nowhere',
error: function(){ alert('error'); }
});
// ...
amplify.request({ resourceId: 'custom' });
The alert never shows. But if I move the error
param to the amplify.request()
call, it works.
See this fiddle (uses HEAD scripts on this github).
I get this error when I switch to private browsing. In normal mode it works just fine. Other browser private browsing do not throw this error.
If you set IE 9 to run in IE 7 mode, then IE claims to support localStorage, but will fail when trying to delete data.
Update this package for 1.1 release
The storage tests fail for me in IE on Windows Phone 7 (device: LG Optimus 7). I've done a bit of digging, and the problem seems to be with the #userData behavior:
var div = document.createElement( "div" ), attrKey = "amplify"; div.style.display = "none"; document.getElementsByTagName( "head" )[ 0 ].appendChild( div ); if ( div.addBehavior ) { div.addBehavior( "#default#userdata" ); // All good so far }
It all works up to this point. (div.addBehavior) evaluates true, and no error is thrown when adding the behavior to the div. However, when you try to load something through the div, I get an error "Object doesn't support this property or method".
div.load( attrKey ); // Throws error
I've spent some time googling for possibilities, but I can't see anything like this reported elsewhere.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.