Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added support for expiring resource remotely

  • Loading branch information...
commit 8cb057caee21f32e7f6e44472ec8c7d0a46b67d6 1 parent c2b5f5e
Shajith Chacko shajith authored
2  AssetFile
View
@@ -2,7 +2,7 @@ require 'rake-pipeline-web-filters'
output "dist"
-files = ["vendor/lru.js", "base.js", "identity_map.js", "ember-resource.js"]
+files = ["vendor/lru.js", "base.js", "remote_expiry.js", "identity_map.js", "ember-resource.js"]
input "src" do
match "{#{files.join(',')}}" do
52 spec/javascripts/remoteExpirySpec.js
View
@@ -0,0 +1,52 @@
+/*globals Ember, jasmine */
+describe('remote expiry', function() {
+ var Resource, resource;
+ describe('on a resource with a remote expiry key', function() {
+ beforeEach(function() {
+ Resource = Ember.Resource.define().extend({
+ remoteExpiryKey: "foo"
+ });
+ resource = Resource.create();
+ spyOn(resource, 'subscribeForExpiry');
+ });
+
+ it('should subscribe for expiry on fetch', function() {
+ Ember.sendEvent(resource, 'didFetch');
+ Ember.run.sync();
+ expect(resource.subscribeForExpiry).toHaveBeenCalled();
+ });
+ });
+
+
+ describe('on a resource with no remote expiry key', function() {
+ beforeEach(function() {
+ Resource = Ember.Resource.define().extend();
+ resource = Resource.create();
+ spyOn(resource, 'subscribeForExpiry');
+ });
+
+ it('should not subscribe for expiry on fetch', function() {
+ Ember.sendEvent(resource, 'didFetch');
+ Ember.run.sync();
+ expect(resource.subscribeForExpiry).not.toHaveBeenCalled();
+ });
+ });
+
+ describe("subscribing for expiry", function() {
+ beforeEach(function() {
+ Resource = Ember.Resource.define().extend({
+ remoteExpiryKey: "foo"
+ });
+ resource = Resource.create();
+ spyOn(Ember.Resource.PushTransport, 'subscribe');
+ Ember.sendEvent(resource, 'didFetch');
+ Ember.run.sync();
+ });
+
+ it('should use the PushTransport', function() {
+ expect(Ember.Resource.PushTransport.subscribe).toHaveBeenCalledWith("foo", jasmine.any(Function));
+ });
+
+ });
+
+});
4 src/ember-resource.js
View
@@ -1035,7 +1035,7 @@
var schema = expandSchema(options.schema);
schema = mergeSchemas(schema, this.schema);
- var klass = this.extend(createSchemaProperties(schema));
+ var klass = this.extend(createSchemaProperties(schema), Ember.Resource.RemoteExpiry);
var classOptions = {
schema: schema
@@ -1102,7 +1102,7 @@
}
}, Ember.Resource.Lifecycle.classMixin);
- Ember.ResourceCollection = Ember.ArrayProxy.extend({
+ Ember.ResourceCollection = Ember.ArrayProxy.extend(Ember.Resource.RemoteExpiry, {
isEmberResourceCollection: true,
type: Ember.required(),
57 src/remote_expiry.js
View
@@ -0,0 +1,57 @@
+(function(exports) {
+
+ var Ember = exports.Ember, NullTransport = {
+ subscribe: Ember.K,
+ unsubscribe: Ember.K
+ };
+
+ Ember.Resource.PushTransport = NullTransport;
+
+ var RemoteExpiry = Ember.Mixin.create({
+ init: function() {
+ var ret = this._super(),
+ self = this,
+ remoteExpiryScope = this.get('remoteExpiryKey');
+
+ if(!this.get('remoteExpiryKey')) { return; }
+
+ this.set('_subscribedForExpiry', false);
+
+ if(!remoteExpiryScope) {
+ return ret;
+ }
+
+ Ember.addListener(this, 'didFetch', this, function() {
+ self.subscribeForExpiry();
+ });
+
+ return ret;
+ },
+
+ subscribeForExpiry: function() {
+ var remoteExpiryScope = this.get('remoteExpiryKey'),
+ updatedAt,
+ self = this;
+
+ if(!remoteExpiryScope) {
+ return;
+ }
+
+ Ember.Resource.PushTransport.subscribe(remoteExpiryScope, function(message) {
+ updatedAt = new Date(message.updated_at);
+ if(self.stale(updatedAt)) {
+ self.set('updatedAt', updatedAt);
+ self.expire();
+ }
+ });
+
+ this.set('_subscribedForExpiry', true);
+ },
+
+ stale: function(updatedAt) {
+ return !this.get('updatedAt') || (+this.get('updatedAt') < +updatedAt);
+ }
+ });
+
+ Ember.Resource.RemoteExpiry = RemoteExpiry;
+}(this));
Please sign in to comment.
Something went wrong with that request. Please try again.