I have a webapp that consists of several pages and a main page. When developing this webapp in Meteor, I segregated the pages into templates. The webapp shall display different pages for a period of time, meaning that Page A shall be for 60 seconds, then proceed with Page B for maybe 75 seconds and so on.
In meteor I construct a main page that consists of header and footer and a changeable templateholder. So basically the main page looks like this:
<template name='main'>
{{>header}}
{{>templateHolder}}
{{>footer}}
</template>
and the Pages are translated into templates, i.e. templateA etc. These templates shall replace the templateHolder based on an update on Session object, which also based on some timing (in seconds) that will be executed by using setTimeout JS function.
Everything works, however I noticed that the timing of each Page goes haywire. When I tested the template individually, they work fine. I suspect that the asynch call of setTimeout somehow conflicting each other.
Here is the JS code that changes the template periodically.
Template.main.templateHolder = function(){
var appIndex = Session.get('currentAppIndex');
switch(appIndex){
case 'A':
return Template['templateA'];
} //....and so on... with other templates
}
Template.main.created = function() {
//Query each pages display time and load it into sessions
// etc...........
Session.set('currentAppIndex',0); //Initialize the first page
setTimeout(nextPage,0);
}
function nextPage() {
//Bunch of line of codes that retrieve time param from settings and calculate
//Also some simple alogrithm to get next templates.. etc
//Update the session index object so that it reactively updates the templateHolder
setTimeout(nextPage,currPageDisplayTime); //currPageDisplayTime is the pages time of display in milliseconds.
}
I am not sure if my way is correct but it managed to display the templates and change them. The only concerns is the timing does not work properly. What is the best way to change template dynamically? Is this prone to any bug in the future?
I have found the reason of this buggy behavior. How do I troubleshoot them?
I put JS alert whenever timeout occurs in templates.
Also, alert whenever a session object is updated.
The flow of the templates rendering should be like this template A (60 secs) --> template B (75secs)--> template A (which is a looping). I noticed that during displaying template B, the alert that I put in template A fires! That means that eventhough only one template is being shown at a time, the created template continues to live healthy behind the scene. It still continues the process whatever it is designated to do.
This made me realize that if both of the templates exist at the same time, I cannot use the same session objects as they will both updating the same things. Turned out that I standardized the use of the session objects across the templates; for example i use Session.set('itemIndex',index) to iterate through the collection in each template, but since the templates are accidentally using the same object, they both update the data at the same time.
So the JS setTimeout works as intended. The problem is the shared session objects.
TL;DR templates don't get destroyed when it is not displayed/changed. This can be avoided by destroying it manually or using different objects.
P/S: Still looking on how to destroy template after created.
Related
Context: Working through the Odin Project and finishing a "ToDo List" application. Trying to implement a functionality where the user will store data locally and be able to re-load data when the app is reloaded.
What I expect: I added 2 functions: saveToLocalStorage and getFromLocalStorage and expect any projects that I create to automatically reload when I refresh the page. They are stored in an object called "allProjects".
What happens instead: The first time a user creates projects, they populate as normal. However, when the page is reloaded, I'm not able to add anymore projects, AND the old projects don't populate the page. I know from console.log statements that the data is being loaded, and am parsing/stringifying the JSONS.
The really weird part: When I examine the allProjects variable in the chrome debugger (by hovering over the variable), I can see that it contains all the project objects I have been creating. However, watching that same object under "watch" it shows up as empty. And when I call it in updateProjectSidebar() as Projects.allProjects (because it is coming from a module), I hover over Projects.allProjects and it is empty there too. I figure this is probably why my sidebar won't render, since it sees nothing in that object to render.
A link to my whole code if you wish to see the whole thing in context.
I figured it out. It was a scope issue. The allProjects var was being returned before getFromLocalStorage() was run.
I am new to Ember, so this might be a stupid question, but bear with me. Is there a specific reason one can't render templates with a dynamic string? Specifically, I push various objects to a controller variable like this:
this.controllerFor('application').get('popups').pushObject({
resource: article,
template: 'article'
});
And then try to render them:
{{#each popup in model}}
{{ render popup.template popup.resource }}
{{/each}}
This doesn't work in Ember as-is, since it expects a string as the template. I just patched this in my ember source in the renderHelper function:
if(name.value) name = name.value();
This makes sure that if the name comes from a property, it gets converted to a string correctly. It works perfectly fine. What is the reason Ember doesn't support this out-of-the-box? Am I missing something?
A little background for the stuff above: I want to open lots of different resources in popups on the page, but I want to keep the popups separated from the rest of the current route so I can show them on their own page if needed without duplicating code. My idea was that I push all open popups to a global array, and they are rendered at the bottom of the page (A bit like we do it at the moment without ember). Maybe there is a better way to do this that I missed!
You want to
Create a popup template independent of route
Pass data to template and render it any where
Right ? If yes there is a very good concept of component in Ember.js
Component is a small module with is separate from application. It only depends upon the data passed into it.
It can be reused in different portion without ever effecting each other. They are especially good to run third party plugins like qtip etc.
here is good article about use case of component.
I'm making a mobile app using Cordova and AngularJS. Currently I have installed ui-router for routing but I'm open to any other alternative for routing.
My desire: I want to cache certain views bound with parameters. In other words I want to cache paths (or pages).
Example situation: let's say that we see some dashboard page, click on some book cover which redirects to the path book/2. This path is being loaded for the first time into app. Router redirects from HomeController to BooksController (whatever the name). Now the BooksController loads data for given $stateParams (book id = 2) and creates view filled with info about chosen book.
What I want in this situation:
I go back to the dashboard page - it is already loaded (cached?)
I choose book #2 again
Controller or router notices that data about this book is already loaded
The view isn't being recreated, instead it's being fetched from cache
Actually, it would be best to cache everything what I visit based on path. Preloading would be cool too.
Reason: performance. When I open some list of books then I want it to show fast. When view is being created every time, then animation of page change looks awful (it's not smooth).
Any help would be appreciated.
EDIT:
First of all, since I believe it's a common problem for many mobile HTML app programmers, I'd like to precise some information:
I'm not looking for hacks but a clear solution if possible.
Data in the views uses AngularJS, so YES, there are things like ng-bind, ng-repeat and so on.
Caching is needed for both data and DOM elements. As far as I know, browser's layout operation is not as expensive as recreating whole DOM tree. And repaint is not what we can omit.
Having separate controllers is a natural thing. Since I could leave without it I cannot imagine how it would work anyway.
I've got some semi-solutions but I'm gonna be strict about my desire.
Solution 1.
Put all views into one file (I may do it using gulp builder) and use ng-show. That's the simplest solution and I don't believe that anyone knowing AngularJS would not think about it.
A nice trick (from #DmitriZaitsev) is to create a helper function to show/hide element based on current location path.
Advantages:
It's easy.
KIND OF preload feature.
Disadvantages:
all views have to be in a single file. Don't ask why it's not convenient.
Since it's all about mobile devices, sometimes I'd like to "clear" memory. The only way I can think of is to remove those children from DOM. Dirty but ok.
I cannot easily cache /book/2 and /book/3 at the same time. I would have to dynamically create DOM children on top of some templates for each view bound with parameters.
Solution 2.
Use Sticky States AND Future States from ui-router-extras which is awesome.
Advantages:
Separated views.
Very clear usage, very simple since it's just a plugin for ui-router.
Can create dynamic substates. So it would be possible to cache book1, book2 but I'm not sure about book/1 and book/2
Disadvantages:
Again, I'm not sure but I didn't found an example with caching a pair/tuple (view, parameters). Other than that it looks cool.
This is precisely the problem I had to solve for my site 33hotels.com. You can check it and play with the tabs "Filter" and "Filter List" (corresponding to different Routes), and see that the View is updated instantly without any delay!
How did I do it? The idea is surprisingly simple - get rid of the Router!
Why? Because the way the Router works is it re-compiles the View upon every single Route change. Yes, Angular does cache the Template but not the compiled View populated with data. Even if data do not change! As the result, when I used the Router in the past, the switch always felt sluggish and non-reactive. Every time I could notice annoying delay, it was a fraction of second but still noticeable.
Now the solution I used? Don't re-compile your Views! Keep them inside your DOM at all times! Then use ng-hide/ng-show to hide/show them depending on the routes:
<div ng-show="routeIs('/dashboard')">
<-- Your template for Dashboard -->
</div>
<div ng-show="routeIs('/book')">
<-- Your template for Book -->
</div>
Then create a function routeIs(string) inside your Controller to test if $location.path() matches string, or begins with string as I am using it. That way I still get my View for all pathes like /book/2. Here is the function I am using:
$scope.routeBegins = function () {
return _.some(arguments, function (string) {
return 0 === $location.path().indexOf(string);
});
};
So no need to be smart with caching - just keep it in the DOM. It will cache your Views for you!
And the best part is - whenever your data is changed, Angular will instantly update all the Views inside your DOM, even the hidden ones!
Why is this awesome? Because, as user, I don't have to wait for all the parsing and compiling at the moment I want to see the result. I want to click the tab and see my results immediately! Why should the site wait for me to click it and then begin all the re-compiling as I am waiting? Especially when this could be easily done before, during the time my computer is idle.
Is there any downside? The only real one I can think of is loading memory with more DOM elements. However, this actual byte size of my views is negligible, comparing e.g. with all JS, CSS and images.
Another possible but avoidable downside is the re-compilation cost of the hidden views. This is where you can get smart and avoid computation-heavy parts depending on the current routes.
Also, you are not re-compiling the whole View, just the parts affected by data changes, which also lowers computational cost.
I find it quite remarkable that everyone is using Routes and seems to be completely unaware (or ignorant) of this problem.
1) About static pages in the app (views), angular takes care of loading them.
for example: for your dashboard page you need not worry about caching the page, as angular will take care of it. Angular will only load the dashboard view once and on all next requests for the dashboard view, angular will just show you the view(not load the file for view), if it is all a static view without any data loaded by ajax calls.
2) if your dashboard is itself loading the book list(or similar data) via ajax, then you can tell your controller to only load the data once and store it to localstorage and on subsequent requests to the dashboard page can only load the data from the localStorage.
3) similar approach can be used when your BooksController loads the data into a view. You can check in your BooksController if the request for a particular book is been previously made and if not your can store it to localstorage or database. and if the data was previously requested then you can load the same data from the storage without making a request to server.
Example situation:
say user makes request for book1, then
your controller i.e BooksController check whether the same data was requested before,
if not, you can load the data via the ajax call from server and also save it to local storage.
if it was loaded before you will load the data stored in the localstorage or in the database.
If you're using ui.router, then you should take a look at ui.router extras, specifically the sticky states module. This allows you to cache the state 'tree' (including rendered views) so they don't have to be compiled or re-rendered on state changes.
http://christopherthielen.github.io/ui-router-extras/
Here's a demo:
http://christopherthielen.github.io/ui-router-extras/example/sticky/#/
I've developed an anti-spam time trap in my app (yes I know it's not going to be 100% effective, but it's a stopgap measure) that does this:
1) When a page is loaded, populate a hidden input with a timestamp using JavaScript:
var timestamp = new Date().getTime() / 1000;
2) When the form is POSTed, use the Python view to check this timestamp against current time -- if it's been less than 5 seconds since the page was loaded, bounce back an exception:
import datetime
feedback_timestamp = request.POST['feedback_timestamp'].strip()
current_timestamp = datetime.datetime.now().timestamp()
if current_timestamp - float(feedback_timestamp) < 5:
raise Exception("Submitted too quickly after loading!")
This works fine on my local development server (i.e. it forces you to wait 5 seconds or else it throws the exception). However, I noticed that once I load it on my production server, the timestamps were about 30 seconds off from one another, even if I submitted the form immediately after loading. It appears that the JavaScript function grabs the time from the local user, while the Python code grabs it from the server so, if they're out of sync, it won't trigger properly.
My question is: how do I grab the initial timestamp from the server, instead of the local machine? Is there a JavaScript function that does this? Or do I need to populate the hidden input using the Python backend instead?
If the latter, things are a bit complicated because this is all being done in my base.html, which is never loaded directly by any of my views (i.e. it's used to extend numerous other views that I use). How would I pull a value from the Python backend without using a view associated with the page?
If you don't want to do the logic in a view, you can send the variable to the template using a context processor:
def add_timestamp_context(request):
return {'current_time': datetime.datetime.now()}
Or something akin to that. See here for more details. You will need to add the new function to your TEMPLATE_CONTEXT_PROCESSORS variable in your settings.py, as noted by the documentation.
You should also note that all variables sent to templates will be available in your base.html. So you can send the variable to the template from the associated view and it'll work, even if the frontend logic is in your base.html file.
The best solution, in my opinion, is to calculate both stamps on the frontend, rather than the backend.
You could use a context processor to add a timestamp context variable to every template. Your context processor is a simple Python function which takes an HttpRequest argument and returns a dictionary which gets added to your context dictionary.
Your function may look like this:
def timestamp(request):
ctx = {
'timestamp': timezone.now()
}
return ctx
Then, in your settings.py add your function to TEMPLATE_CONTEXT_PROCESSORS:
TEMPLATE_CONTEXT_PROCESSORS = (
...,
'path.to.function.context_processors.timestamp',
)
And that's it! See further documentation here.
I'm trying to load and parse json data from an external source into a table via Knockout JS. So far, everything has been successful through the following code:
// Snippet
var self = this;
self.notices = ko.observableArray([]);
self.currentTab = ko.observable(5);
ko.computed(function() {
$.getJSON('http://json.source.here.com/tab/'+ko.toJS(self.currentTab), function(threads) {
if (threads !== null) {
self.notices(threads);
} else {
self.notices([]);
}
});
}, self.notices);
When a user clicks on a certain tab it would load the json data (forum threads) based on the selected tab value (self.currentTab) onto the table in the form of rows (self.notices).
Everything works as expected however, I noticed while browsing around other pages that do not have the above bindings, the json is still being loaded ($.getJSON is fired). I'm concerned that this may have some detrimental effects on the performance of my website as it is loading the json source even though it is not needed.
EDIT: I figured this out through Google Chrome's developer console.
I currently have my view model in a JavaScript file that is being used by every other pages as well. It consists of bindings for all the pages.
My question is, how can I load the json data on a specific page or only when the bindings are present - lazy loading? Preferably, I would like to keep all the bindings in a single JavaScript file, I do not want to separate them out and load them on a per page basis.
Here is an article that I wrote on a simliar topic a little while back: http://www.knockmeout.net/2011/06/lazy-loading-observable-in-knockoutjs.html
In your case, I think that you really want to add some guards around the $.getJSON call to ensure that it is only making AJAX requests when you are in the appropriate state (on the appropriate tab).
Along with that, the blog post describes using the deferEvaluation flag on a computed observable to ensure that the logic does not run until someone binds against the computed observable (in your case, you have an anonymous computed observable, but you could add it to your view model as a property and bind against it in your view. Without this flag, the evaluation code will run when you create the computed observable, which is not desirable in your case.