I am trying to get my head round the connectOutlet method and when a view that is returned from connectOutet is actually inserted into the DOM.
The view that is created in connectOutlet leaves connectOutlet in the preRender state.
connectOutlet: function(name, context) {
// method body
view = this.createOutletView(outletName, viewClass);
if (controller) { set(view, 'controller', controller); }
set(this, outletName, view);
return view;
}
I've not tracked down where or when the view is inserted into the Dom and the view transitions to the inDom state.
I suspect the runloop is at play and it transitions after the current runloop has finished.
Can anyone shed any light on this?
The run loop is indeed in play here. The run loop processes events by draining an ordered collection of queues. In order, they are: sync, actions, render, afterRender, destroy, and timers. View rendering is where the view is actually inserted into the DOM and it is always scheduled on the render queue.
If you have other questions about this, leave a comment, and I will be happy to expand this answer to cover them.
Related
What is the best technique to generically change to a busy cursor during transition to a new route in Ember.js (1.13.10)? With data retrieval, this may take a couple of seconds.
The answer here indicates a method to do this with saving data:
Changing mouse cursor while Ember content is saving
...and indicates this is very straight forward with route transitions, but I can't seem to find an example or anything in the documentation.
Thanks in advance for any help on this.
We can use loading hook of Route.
app/routes/application.js:
import Ember from 'ember';
export default Ember.Route.extend({
actions: {
loading(transition, route) {
$('body').css({ cursor : 'wait' });
this.router.one('didTransition', () => {
$('body').css({ cursor: 'default' });
});
return true; // Bubble the loading event
}
}
});
Working demo. (output is sandboxed so make sure your mouse is over body in output window)
Full code behind demo.
I've used $('body') as element to style cursor. You can probably use more global approach.
How can we handle back button press in android, as of now I am catching backButtonPress event with following code :
document.addEventListener("backbutton", function({
document.location.reload(true);
},false);
I am trying improve this implementation by pushing renderController and the current view it is showing to the array and popping them on press of a back button. But the problem here is, I am not able to get current view that render controller is showing.
Is there any way we can get the current render node the RenderController is showing?
Is their any other way I can handle android back button press event?
There is not a way to get the current renderable from the RenderController
You could keep track of the renderable you passed using the show method.
OR
You could extend the RenderController
var RenderController = require("famous/views/RenderController");
RenderController.prototype.getRenderNode = function getRenderNode() {
return this._renderables[this._showing];
}
Later you can call the method and get the render node passed with the show method.
var node = renderController.getRenderNode();
So, I was hoping to be able to create a helper (master) view for my application, the main reason is that I want to implement a resize function.
Example.
App.View = Ember.View.extend({
init: function() {
$(window).resize(this.resize.bind(this));
this._super();
}
});
And then extend it
App.AmazingView = App.View.extend({
resizing: false,
resize: function() {
this.set('resize', true);
}
});
So, that's an example of what I wanted to achieve, but unfortunately it causes some problems.
If I go to the route that is using the Amazing view then everything works fine, it's only until you navigate around the application after and then return to that same route I face the following error.
Uncaught Error: Something you did caused a view to re-render after it rendered but before it was inserted into the DOM.
I'm pretty sure this is happening because I'm extending that view, any ideas on why this happens or what I should actually be doing?
When you do $(window).resize(this.resize.bind(this)); you are attaching an event not managed for ember. When you leave the view, transitioning to other route, ember will cleanup event listeners and destroy the view. Now your view is in a "dead" state. But the resize handler is still present and when it triggers, a change is performed in a dead view. I think that it is causing your problem.
You can use the didInsertElement and willClearRender to bind and unbind your event.
App.View = Ember.View.extend({
_resizeHandler: null,
didInsertElement: function() {
this._super();
// we save the binded resize function to keep the identity, so unbind will remove
this._resizeHandler = this.resize.bind(this);
$(window).bind('resize', this._resizeHandler);
},
willClearRender: function() {
this._super();
$(window).unbind('resize', this._resizeHandler);
}
});
This sound like a feature that would fit perfectly into an ember mixin. This solution would offer more flexibility, then you could extend other views and also provide the resize features...
http://emberjs.com/api/classes/Ember.Mixin.html
I'd like to know what the best approach is to delay the loading of my model data till after an animation has completed.
In my application I'm listening to changes to the route. Then, when a certain route is matched, I open a panel with an animation. But I'd like the loading of the data to start after the animation completed.
Here's how I listen to the route:
App.ApplicationController.reopen({
currentPathChanged:function() {
App.panelView.setNeedsOpen(this.get('currentPath') == 'index' ? false : true);
}.observes('currentPath')
});
And this is one of the routes that is called when the panel has to open:
App.NewsitemsRoute = Ember.Route.extend({
model: function() {
return App.Newsitem.find();
}
});
But as you can see, the data gets preloaded when the animation is still running. How should I solve this?
I would not at all delay loading itself as you should use this precious time to load data in background and 'distract' the user with the animation.
So why are you not showing the animation but load the data in parallel and as soon as the animation is finished you remove the corresponding dom revealing the rendered data?
It seems like if you want to animate a transition between states using the new Ember.js router and outlets, you're out of luck, since the previous content of an outlet will be destroyed before you have a chance to animate it. In cases where you can completely animate one view out before transitioning to the new state, there's no problem. It's only the case where both old and new views need to be visible that's problematic.
It looks like some of the functionality needed to animate both the previous outlet content and the new was added in this commit, but I'm not sure I understand how to use it.
There's also been some discussion about using extra transitional routes/states to explicitly model the "in-between" states that animations can represent (here and here), but I'm not sure if it's currently possible to match this approach up with outletted controllers and views.
This is similar to How *not* to destroy View when exiting a route in Ember.js, but I'm not sure overriding the outlet helper is a good solution.
Any ideas?
I am currently overriding didInsertElement and willDestroyElement in some of my view classes to support animations. didInsertElement immediately hides the element and then animates it into view. willDestroyElement clones the element and animates it out of view.
didInsertElement: function ()
{
this.$().slideUp(0);
this.$().slideDown(250, "easeInOutQuad");
},
willDestroyElement: function ()
{
var clone = this.$().clone();
this.$().replaceWith(clone);
clone.slideUp(250, "easeInOutQuad");
}
Personally, I don't want to start wrapping my outlets in superfluous ContainerViews just to support animations.
You should check this out: https://github.com/billysbilling/ember-animated-outlet.
Then you can do this in your Handlebars templates:
{{animatedOutlet name="main"}}
And transition from within a route like this:
App.ApplicationRoute = Ember.Route.extend({
showInvoice: function(invoice) {
this.transitionToAnimated('invoices.show', {main: 'slideLeft'}, invoice);
}
});