Permalink
Browse files

Klient henter nå bygningen fra serveren.

  • Loading branch information...
1 parent 8a63086 commit c7ae72329b8a467d0ddea81b896c41ae54959476 @magnars committed Jun 24, 2012
Showing with 74 additions and 26 deletions.
  1. +4 −0 app.js
  2. +13 −0 lib/client/server.js
  3. +17 −18 public/scripts/building-page.js
  4. +31 −0 test/client/server-test.js
  5. +8 −8 test/integration/test-helper.js
  6. +1 −0 views/layout.jade
View
4 app.js
@@ -56,6 +56,10 @@ app.get("/", function (req, res) {
});
});
+app.get("/current-building.json", function (req, res) {
+ res.send(JSON.stringify(building));
+});
+
bayeux.attach(app);
app.listen(3000);
View
@@ -0,0 +1,13 @@
+var ZOMBIE = this.ZOMBIE || {};
+
+(function (Z) {
+
+ var urlPrefix = function () { return Z.urlPrefix || ""; };
+
+ Z.server = {
+ getCurrentBuildingProperties: function (callback) {
+ jQuery.getJSON(urlPrefix() + "/current-building.json", callback);
+ }
+ };
+
+}(ZOMBIE));
@@ -8,28 +8,27 @@ ZOMBIE.pageInitialized = (function (Z) {
hub: hub
}).init();
- var building = Z.building.create({
- zombies: 50,
- barricade: 7,
- rooms: [ Z.rooms.trapdoor.create() ],
- sleepers: 4
- });
+ var d = when.defer();
- Z.hub = hub;
+ Z.server.getCurrentBuildingProperties(function (props) {
+ var building = Z.building.create(props);
- var controller = Z.buildingController.create({
- building: building,
- hub: hub
- });
+ var controller = Z.buildingController.create({
+ building: building,
+ hub: hub
+ });
+
+ controller.on("change", function (building) {
+ Z.updateBuildingView(
+ document.getElementById("building"),
+ Z.renderBuilding,
+ building
+ );
+ });
- controller.on("change", function (building) {
- Z.updateBuildingView(
- document.getElementById("building"),
- Z.renderBuilding,
- building
- );
+ controller.init().then(d.resolve);
});
- return controller.init();
+ return d.promise;
}(ZOMBIE));
View
@@ -0,0 +1,31 @@
+(function (Z) {
+ "use strict";
+
+ buster.testCase('Server', {
+ setUp: function () {
+ this.stub(jQuery, "getJSON");
+ },
+
+ "delegates to jQuery": function () {
+ Z.server.getCurrentBuildingProperties(this.spy());
+
+ assert.calledOnceWith(jQuery.getJSON, "/current-building.json");
+ },
+
+ "uses urlPrefix if any": function () {
+ Z.urlPrefix = "test";
+ Z.server.getCurrentBuildingProperties(this.spy());
+
+ assert.calledOnceWith(jQuery.getJSON, "test/current-building.json");
+ },
+
+ "calls back": function () {
+ var callback = this.spy();
+ Z.server.getCurrentBuildingProperties(callback);
+
+ jQuery.getJSON.invokeCallback({ yaba: "dabadoo" });
+
+ assert.calledOnceWith(callback, { yaba: "dabadoo" });
+ }
+ });
+}(ZOMBIE));
@@ -1,9 +1,9 @@
var testHelper = {};
-(function () {
- "use strict";
+var ZOMBIE = { urlPrefix: "app" };
- var pathPrefix = "app";
+(function (Z) {
+ "use strict";
var head = document.getElementsByTagName("head")[0] || document.documentElement;
@@ -42,21 +42,21 @@ var testHelper = {};
var loadScriptsIn = function (html, callback) {
var matches = html.match(/script src="[^"]+/g);
- matches = matches.map(function (s) { return pathPrefix + s.substring(12); });
+ matches = matches.map(function (s) { return Z.urlPrefix + s.substring(12); });
loadScriptsSerially(matches, callback);
};
testHelper.loadPage = function (url, callback) {
- jQuery.get(pathPrefix + url, function (html) {
+ jQuery.get(Z.urlPrefix + url, function (html) {
document.body.innerHTML = html;
loadScriptsIn(html, function () {
- ZOMBIE.pageInitialized.then(callback);
+ Z.pageInitialized.then(callback);
});
});
};
testHelper.getPageContents = function (url, callback) {
- jQuery.get(pathPrefix + url, callback);
+ jQuery.get(Z.urlPrefix + url, callback);
};
-}());
+}(ZOMBIE));
View
@@ -17,6 +17,7 @@ html
script(src='/shared/building.js')
script(src='/shared/building-renderer.js')
script(src='/shared/building-controller.js')
+ script(src='/client/server.js')
script(src='/client/update-building-view.js')
script(src='/client/blueprint-controller.js')
script(src='/scripts/building-page.js')

0 comments on commit c7ae723

Please sign in to comment.