Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
First initial support for cookies. Very basic at this point.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanpalmer committed Aug 20, 2012
1 parent 1586938 commit 5cb6dfe
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 2 deletions.
38 changes: 38 additions & 0 deletions spec/SpineDataBindSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1467,4 +1467,42 @@ describe("Spine.DataBind", function() {
Tests();
});
});

describe("Cookie", function() {
var Tests = function() {
it("should set the property from a cookie", function() {
expect(Person.firstName).toBe("Eric");
});

it("should set the cookie when model gets updated", function() {
Person.firstName = "Bender";
if (!Watch) Person.save();

expect(document.cookie).toBe("firstName=Bender");
});
};

describe("with bindings", function() {
beforeEach(function() {
document.cookie = "firstName=Eric"

PersonController.include({
bindings: {
"cookie firstName": "firstName",
}
});

Watch = false;
Person = PersonCollection.create({
firstName: "Nathan",
lastName: "Palmer",
title: "Mr"
});

Controller = PersonController.init({ el: 'body', model:Person });
});

Tests();
});
});
});
40 changes: 40 additions & 0 deletions src/spine.databind.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,45 @@ class Hash extends Template
change: (operators,model,controller,el,options,hash) ->
@disable => @set(model,operator.property,hash[operator.target]) for operator in operators

class Cookie extends Template
keys: [ "cookie" ]

@get: (sKey) ->
unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1"))

@set: (sKey, sValue, vEnd, sPath, sDomain, bSecure) ->
return if not sKey or /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)
sExpires = ""
if vEnd?
switch vEnd.constructor
when Number
sExpires = if vEnd is Infinity then "; expires=Tue, 19 Jan 2038 03:14:07 GMT" else "; max-age=" + vEnd
when String
sExpires = "; expires=" + vEnd
when Date
sExpires = "; expires=" + vEnd.toGMTString()

document.cookie = escape(sKey) + "=" + escape(sValue) + sExpires + (if sDomain then "; domain=" + sDomain else "") + (if sPath then "; path=" + sPath else "") + (if bSecure then "; secure" else "")

bind: (operators,model,controller,el,options) ->
if options.watch
@bindToModel([operator],model,controller,el,options,"update["+operator.property+"]") for operator in operators
else
@bindToModel(operators,model,controller,el,options,"change")

@change(operators,model,controller,el,options)

update: (operators,model,controller,el,options) ->
for operator in operators
value = @get(model,operator.property)
Cookie.set(operator.target,value)

change: (operators,model,controller,el,options) ->
for operator in operators
value = Cookie.get(operator.target)
binder.set(model,operator.property,value,options)


DataBind =
binders: [
new Update()
Expand All @@ -429,6 +468,7 @@ DataBind =
new Attribute()
new Checked()
new Hash()
new Cookie()
]

refreshBindings: (model) ->
Expand Down
78 changes: 76 additions & 2 deletions src/spine.databind.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5cb6dfe

Please sign in to comment.