From 563935933c191093b8ccd88fd1377775c5641b0d Mon Sep 17 00:00:00 2001 From: Valerio Virgillito Date: Mon, 4 Jun 2012 23:33:32 -0700 Subject: components fix: adding a first draw event to components Adding a first draw allows the component to have the right element so that ninja can use it's controller to get the component instance Signed-off-by: Valerio Virgillito --- js/document/templates/app/main.js | 52 ++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 9 deletions(-) (limited to 'js/document') diff --git a/js/document/templates/app/main.js b/js/document/templates/app/main.js index a406abdb..fbd2e138 100644 --- a/js/document/templates/app/main.js +++ b/js/document/templates/app/main.js @@ -8,12 +8,38 @@ var Montage = require("montage/core/core").Montage, Template = require("montage/ui/template").Template, TemplateCreator = require("tools/template/template-creator").TemplateCreator; + +//var njmodelGet = function njmodelGet() { +// return (this.hasOwnProperty("_model") ? this._model: document.modelGenerator.call(this)); +//}; +// +//Object.defineProperty(Object.prototype, "_model", { +// enumerable: false, +// value: null, +// writable: true +//}); +// +//Object.defineProperty(Object.prototype, "elementModel", { +// configurable: true, +// get: njmodelGet, +// set: function() { +// } +//}); + exports.Main = Montage.create(Component, { hasTemplate: { value: false }, + componentToInsert: { + value: null + }, + + firstDrawCallback: { + value: null + }, + /** * Adding window hooks to callback into this object from Ninja. */ @@ -24,6 +50,12 @@ exports.Main = Montage.create(Component, { window.addComponent = function(element, data, callback) { var component; + if(!self.firstDrawCallback) { + self.firstDrawCallback = {}; + self.firstDrawCallback.callback = data.firstDraw.cb; + self.firstDrawCallback.context = data.firstDraw.ctx; + } + component = require.async(data.path) .then(function(component) { var componentRequire = component[data.name]; @@ -34,6 +66,9 @@ exports.Main = Montage.create(Component, { componentInstance.needsDraw = true; componentInstance.ownerComponent = self; + self.componentToInsert = componentInstance; + componentInstance.addEventListener("firstDraw", self, false); + callback(componentInstance, element); }) .end(); @@ -46,16 +81,15 @@ exports.Main = Montage.create(Component, { // var templateEvent = document.createEvent("CustomEvent"); templateEvent.initCustomEvent("mjsTemplateReady", false, true); - document.body.dispatchEvent(templateEvent); - - - // Dispatch event when this template has loaded. - /* - var newEvent = document.createEvent( "CustomEvent" ); - newEvent.initCustomEvent( "userTemplateDidLoad", false, true ); - document.body.dispatchEvent( newEvent ); - */ + } + }, + + handleFirstDraw: { + value: function() { + this.firstDrawCallback.callback.call(this.firstDrawCallback.context, this.componentToInsert); + this.componentToInsert.removeEventListener("firstDraw", this, false); + this.componentToInsert = null; } } }); \ No newline at end of file -- cgit v1.2.3