Notice: The Monaca & Onsen UI Community Forum is shutting down.
For Onsen UI bug reports, feature requests and questions, please use the Onsen UI GitHub issues page. For help with Monaca, please contact Monaca Support Team.
Thank you to all our community for your contributions to the forum. We look forward to hearing from you in the new communication channels.
I'm new! Onsen 2.0 questions
-
Hello all,
I’m really enjoying working with Onsen and have a few questions I hope someone can answer.
-
I added an “init” event listener to be able to bind element events when a page is initialized. I was wondering :
When a page is popped out of the stack, what exactly happens to the page? Do all the events get unbinded to free up memory? Do I have to rebind events if the page is re-called? -
Should all the javascript files for each page be loaded at once when the app starts? Or placed in each individual page somehow?
-
Do all html pages need to have the ons-navigator component for navigation? Or can the ons-navigator be placed only on the startup page?
-
Since Onsen 2.0, I don’t need to use angular. Could someone recommend a good lightweight 2 way binding framework?
Thanks everyone!
Daniel
-
-
Welcome @Dan! These are just my opinions regarding your questions:
-
I would think the event listener remains. The elements are just removed from the DOM. As far as best practice goes, you should always remove listeners if they are no longer needed.
-
You can load JS when you need it, but you get errors if you try to add them to the template pages, so you will do this all through your index.html.
-
No (depends on your navigation model). In general, its one navigator on the index.html that you push pages too.
-
I am the wrong guy for this because I believe in Vanilla JS all the way. No frameworks for me! Just use a listener to update your data.
Hope this helps you with your endeavor and please feel free to ask away!
Mark
-
-
Welcome aboard @Dan ! Glad you have you here :)
I’ll try to answer your questions:
-
You didn’t add the
init
event listener toons-page
itself, right? You added it to thedocument
or perhaps another parent component. Therefore, page removal has nothing to do with that event listener. You can remove it manually if you consider you don’t need it anymore. Anyway it should be just 1 listener. You can check this code to see an example. -
For now JavaScript files should be included in
index.html
always until ES6 has better support. This is rather an HTML/JavaScript limitation than anything related to Onsen UI. -
Not at all.
ons-navigator
is just one of the “navigation components” (navigator, tabbar, splitter) in Onsen UI and in general you need only one (normally located inindex.html
as the first element, but it depends on your app). Let’s say these navigation components are “frames” that are able to change their inner content. What you need to define in the html pages is precisely that content in the shape of differentons-page
. So basically, you can think inons-page
as the minimum unit of these navigation components, although it’s also possible to load a navigation component inside another one. -
I found some time ago Bind.js but I haven’t tried it out myself. It would be great if you want to try it and comment here your experience. Apart from that, I think Vue.js plays nicely with Onsen UI and has two-way bindings if I’m not wrong.
Hope this helps :grin:
–
@munsterlander Looks like I’m too slow answering… hahah
-
-
@Fran-Diox HA! I am just glad my answers match yours for the most part! Always good when what I say matches up with the devs! Thanks for pointing out those two libraries, I am going to give them a look as I have one project where it could be of use instead of doing my normal manual listener model.
-
Thank you both for your helpful responses!
Fran, I added the init event listener to the document yes, I use the init event listener to instantiate a plugin or bind events to a specific page once it’s done loading. Ex :
document.addEventListener("init", function (e) { if (e.target.id == "schedule") { $('.inputDate').mobiscroll().date({ theme: 'ios', minDate: new Date(), dateFormat: 'yy-mm-dd' }); } });
Is this the correct approach?
From what I am understanding, once a page is popped out, the event listeners remain and need to be manually removed.Dan
-
@Dan You are correct!