Permalink
Browse files

added i-bem__json_defer_yes

  • Loading branch information...
1 parent 95a9d70 commit 5fb23eb417ba1b8212847909e64d4c02c05abf67 @wtfil wtfil committed Mar 29, 2013
@@ -0,0 +1,39 @@
+/**
+ * Defered declarations (ctx.defer) for BEM.JSON
+*/
+(function (BEM) {
+
+ var wait = BEM.JSON._ctx.prototype.wait,
+ resume = BEM.JSON._ctx.prototype.resume;
+
+
+ BEM.JSON._ctx.prototype.wait = function () {
+ console.error('ctx.wait() is deprecated, use ctx.defer()');
+ wait.call(this);
+ };
+
+ BEM.JSON._ctx.prototype.resume = function () {
+ console.error('ctx.resume() is deprecated, use ctx.defer()');
+ resume.call(this);
+ };
+
+ /**
+ * Continue build after promise fulfills or removes blocks after reject
+ *
+ * @param {Vow.promise} promise
+ * @param {Vow.promise} promise.fulfill to continue with other block declarations
+ * @param {Vow.promise} promise.reject to remove block and continue render
+ */
+ BEM.JSON._ctx.prototype.defer = function (promise) {
+ var ctx = this;
+ wait.call(ctx);
+ promise.fail(function (err) {
+ if (err && err.message) {
+ console.error(err);
+ }
+ ctx.remove();
+ }).always(function () {
+ resume.call(ctx);
+ }).done();
+ };
+}(BEM));
@@ -0,0 +1,5 @@
+({
+ mustDeps: [
+ {block: 'i-bem', elem: 'json', mods: {async: 'yes'}}
+ ]
+})
@@ -0,0 +1,123 @@
+/*global describe: false, it: false, waitsFor: false, runs: false, expect: false */
+/**
+ * Test for ctx.defer in BEM.JSON
+ */
+describe('_defer bem-json', function () {
+
+ var fulfillAsync = function (content) {
+ var promise = Vow.promise();
+ setTimeout(function () {
+ promise.fulfill(content);
+ }, 100);
+ return promise;
+ }, rejectAsync = function (content) {
+ var promise = Vow.promise();
+ setTimeout(function () {
+ promise.reject(content);
+ }, 100);
+ return promise;
+ };
+
+ BEM.JSON.decl('b-test-defer', {
+ onBlock: function (ctx) {
+ ctx.content(
+ {elem: 'item', data: ctx.params().data}
+ );
+ },
+
+ onElem: {
+ item: function (ctx) {
+ ctx.content(ctx.params().data);
+ }
+ }
+ });
+
+ it('base usage', function () {
+ var bemjson;
+
+ BEM.JSON.decl({name: 'b-test-defer', modName: 'test', modVal: 'base'}, {
+ onBlock: function (ctx) {
+ ctx.defer(fulfillAsync('test string').then(function (content) {
+ ctx.param('data', content);
+ }));
+ }
+ });
+
+ BEM.JSON.buildAsync({block: 'b-test-defer', mods: {test: 'base'}}, function (bemjsonParam) {
+ bemjson = bemjsonParam;
+ });
+
+ waitsFor(function () {
+ return bemjson;
+ }, 'bemjson never builded', 1000);
+
+ runs(function () {
+ expect(bemjson).toBeDefined();
+ expect(bemjson.content).toBeDefined();
+ expect(bemjson.content.elem).toBe('item');
+ expect(bemjson.content.content).toBe('test string');
+ });
+ });
+
+ it('defered elem', function () {
+ var bemjson, test = 'defered-elem';
+
+ BEM.JSON.decl({name: 'b-test-defer', modName: 'test', modVal: test}, {
+ onBlock: function (ctx) {
+ ctx.defer(fulfillAsync('test string').then(function (content) {
+ ctx.param('data', content);
+ }));
+ },
+ onElem: {
+ item: function (ctx) {
+ ctx.defer(fulfillAsync(ctx.params().data + ' defered').then(function (content) {
+ ctx.param('data', content, true);
+ }));
+ }
+ }
+ });
+
+ BEM.JSON.buildAsync({block: 'b-test-defer', mods: {test: test}}, function (bemjsonParam) {
+ bemjson = bemjsonParam;
+ });
+
+ waitsFor(function () {
+ return bemjson;
+ }, 'bemjson never builded', 1000);
+
+ runs(function () {
+ expect(bemjson).toBeDefined();
+ expect(bemjson.content).toBeDefined();
+ expect(bemjson.content.elem).toBe('item');
+ expect(bemjson.content.content).toBe('test string defered');
+ });
+ });
+
+ it('rejected block', function () {
+ var bemjson, test = 'rejected-block';
+
+ BEM.JSON.decl({name: 'b-test-defer', modName: 'test', modVal: test}, {
+ onBlock: function (ctx) {
+ ctx.defer(rejectAsync('test string').then(function (content) {
+ ctx.param('data', content);
+ }));
+ }
+ });
+
+ BEM.JSON.buildAsync({
+ block: 'b-test-defer-wraper',
+ content: {block: 'b-test-defer', mods: {test: test}}
+ }, function (bemjsonParam) {
+ bemjson = bemjsonParam;
+ });
+
+ waitsFor(function () {
+ return bemjson;
+ }, 'bemjson never builded', 1000);
+
+ runs(function () {
+ expect(bemjson).toBeDefined();
+ expect(bemjson.content).toBe(null);
+ });
+ });
+});
View
@@ -1,6 +1,6 @@
{
"name": "bem-node",
- "version": "0.0.3",
+ "version": "0.0.4",
"description": "",
"keywords": [
"bem",

0 comments on commit 5fb23eb

Please sign in to comment.