Skip to content

Commit

Permalink
added save method - doesn't save yet
Browse files Browse the repository at this point in the history
  • Loading branch information
crisward committed Apr 2, 2016
1 parent 0b21de6 commit 4f9a729
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
16 changes: 15 additions & 1 deletion lib/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,25 @@ this.load = (function(_this) {
xhr = new XMLHttpRequest();
xhr.open('GET', encodeURI(opts.src));
xhr.onload = function() {
var res;
if (xhr.status === 200) {
return _this[opts.data] = JSON.parse(xhr.responseText);
res = JSON.parse(xhr.responseText);
if (Array.isArray(res)) {
return _this[opts.data] = res.map(function(row) {
row.$save = _this.save;
return row;
});
} else {
res.$save = _this.save;
return res;
}
}
};
return xhr.send();
};
})(this);

this.save = (function(_this) {
return function() {};
})(this);
});
22 changes: 17 additions & 5 deletions src/rest.tag
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ rest
script(type='coffee').

@on 'mount',->
@load() if opts.src
@load(opts.src) if opts.src

@load = =>
@load = (url)=>
xhr = new XMLHttpRequest()
xhr.open('GET', encodeURI(opts.src))
xhr.onload = => if xhr.status==200 then @[opts.data] = JSON.parse(xhr.responseText)
xhr.send()
xhr.open('GET', encodeURI(url))
xhr.onload = =>
if xhr.status==200
res = JSON.parse(xhr.responseText)
if Array.isArray(res)
@[opts.data] = res.map (row)=>
row.$save = @save
return row
else
res.$save = @save
return res
xhr.send()

@save = =>

22 changes: 13 additions & 9 deletions test/rest.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ datagen = require './mockdata'
domnode = null
tag = null
opts = {src:"/api/users",data:"users"}
requests = null

if !('remove' in window.Element.prototype)
window.Element.prototype.remove = -> if @parentNode then @parentNode.removeChild(@)
Expand All @@ -12,8 +13,8 @@ describe 'rest',->

beforeEach ->
@xhr = sinon.useFakeXMLHttpRequest()
@requests = []
@xhr.onCreate = (xhr)=> @requests.push(xhr)
requests = []
@xhr.onCreate = (xhr)-> requests.push(xhr)
domnode = document.createElement('div')
domnode.appendChild(document.createElement('rest'))
node = document.body.appendChild(domnode)
Expand All @@ -26,11 +27,14 @@ describe 'rest',->

it "should exit",->
expect(document.querySelector('rest')).to.not.be.null

describe "after data loaded",->

it "should load data",->
expect(@requests.length).to.equal(1)

it "should have access to data",->
@requests[0].respond(200, {"Content-Type": "application/json"},JSON.stringify(datagen(10)))
expect(tag.users.length).to.equal(10)

beforeEach ->
requests[0].respond(200, {"Content-Type": "application/json"},JSON.stringify(datagen(10)))

it "should have access to data",->
expect(tag.users.length).to.equal(10)

it "should have a $save method on each element in the collection",->
expect(tag.users[0].$save).to.exist

0 comments on commit 4f9a729

Please sign in to comment.