Polymer provides automatic two-way binding between template and data which is great. It also provides a one time binding. Is there a way to have a one-time binding with a way to refresh it later? I like the expressiveness of the polymer template, but I am not sure about performance of polling. (we have really complicated dom). We are investigating possibility of doing FRP (possibly Bacon.js) in Polymer.
Thanks in advance!
My reaction to questions like this is usually: have you actually observed a performance issues?
Keep in mind, if the browser supports Object.observe(), there's no polling. If that feature isn't available, Polymer does default to polling in order to flush the data-binding system and propagate changes. That doesn't necessarily mean DOM changes either. Polymer's data binding system makes the minimal amount of DOM changes to reflect model changes. See the section on maintaining instances as well.
Related
Sorry if this is a generic question, but I have been given a task to develop a web application, and I'd like to use this opportunity to dive into and learn about Polymer (and maybe Vaadin components?).
I'd like to avoid reinventing the wheel. But I'm a newbie regarding Polymer. So, given the following task, is there any approach and component that will make the developing quicker/smarter?
Create an application which will allow at least two users to log in simultaneously and manage items in categories. The categories should be in a hierarchy of potentially infinite depth. The items only require a label.
The users should be able to perform standard CRUD, plus if one user makes a change, the other user(s) should see the change (if appropriate) without manually refreshing their web browser.
How should I approach this with Polymer?
Has anyone done anything similar?
I'm also open to Vaadin components if it helps.
Any help or guideline?
🙏
One way would be to start from an application template like polymer3-webpack-starter or pwa-starter-kit.
If you are looking to use Vaadin components like vaadin-grid, use polymer3-webpack-starter or one of the starters from the vaadin.com/start page.
If you do not need an example specifically with Vaadin components, then pwa-starter-kit would be a good starting point. Though it assumes familiarity with redux.
Pro: You can quickly get a running application that you can modify to your needs, and you do not have to set a project from scratch (build tool chain, module bundler, tests, configuration, etc - all of that is done already).
Con: Making modifications to the project setup won't be necessarily easy because at that point you will have to dive into the project setup that somebody has done for you.
We wrote code with FB React before, using a single immutable app-state. However, we did the rendering like:
model = immutable({name:"X"});
function change_name(name){
swap(render(change("name", name, model))))
}
where render:
function render(state){
ReactDOM.render(<Todos app_state={state} />,document.getElementById('main'));
}
In reagent we use a r/atom, which on every swap will check whether we need to do another render. For very simple stuff, like the one above, that's ok, but if the operations are very complex, including a lot of back and forth ajax operations, controlling manually when the render should occur is better.
The TODOMVC of how we worked before is here: http://jsfiddle.net/danbunea1/bL62p47n/
As far as I know reagent batches changes with requestAnimationFrame, and the components implement a smart shouldComponentUpdate so I'd say that the defaults are very performance sensible and I'd suggest not prematurely optimizing.
That said, for accomplishing what you just asked, just perform the atom mutations after you've done all those expensive operations.
That way only when you swap!, reset!, etc at the end, will reagent consider triggering renders.
I am a bit confused about which invalidate method to use and when to use it. I need to change x and y of a component and in that case one should call a invalidation method for optimization, but i don't know which one and when exactly
target.addElement(node);
node.x = 100 + target.horizontalScrollPosition;
node.y = 100 + target.verticalScrollPosition;
node and target are both Groups
It depends on the component and perhaps you don't have to call any at all. From the given piece of code i'd say it's invalidateSize(). But containers usually make a good job of measuring their dimensions property. invalidateDisplayListmight be a good call, if you need to change the way the component is displayed.
So, generally speaking, it depends on the component (super type etc) you are implementing.
Edit:
As both instances are groups, you shouldn't call any invalidation methods at all. You would only call the methods when implementing a custom component with additional properties. In the case of Groups, anything has been done for you in advance. The component live cycle is implemented and the various layouts provide a comfortable level of indirection.
When you extend Group (or any other component) then you should be familiar with the component live cycle.
Rule of thumbs:
ignore the invalidation calls in pure MXML as it is done by the MXML compiler and the components themselves.
use the invalidation calls in overridden setters, which mutate the state of the component (even in MXML). This usually leads to a clean yet simple design of components if the setters are used everywhere - even inside the components private methods.
use validateSize, validateNow etc carefully, as these are simple synchronous shortcuts avoiding the component live cycle.
The invalidation live cycles is based on the flash players elastic race track, which which divides rendering and processing of the data in different aspects processing the code.
Further readings regarding the idea behind the invalidation calls:
Updated elastic racetrack[1] and The Elastic racetrack[2]
[1]: http://www.craftymind.com/updated-elastic-racetrack-for-flash-9-and-avm2/
[2]: http://tedpatrick.com/2005/07/19/flash-player-mental-model-the-elastic-racetrack/
I'm starting to use Dojo; this is (essentially) my introduction to AJAX. We have a Java backend (torque / turbine / velocity) and are using the jabsorb JSON-RPC library to bridge Java and Javascript.
What do I need to know? What is the big picture of Dojo and JSON, and what are the nasty little details that will catch me up? What did you spend a couple of days tracking down, when you started with Dojo, that you now take for granted? Thanks for any and all tips.
The first thing to do is get familiar with the Dojo Object Model. JavaScript does not have a class system so the Dojo toolkit has created a sort of "by convention" object model that works rather well but is very different to how it works in Java for example.
The reason I suggest getting familiar with it is so you can dig into the code base whenever you start experiencing issues. The documentation available has improved significantly over the past year, but every now and then I find myself having to work out a bug in my code by learning exactly how the Dojo code involved works.
Another tip is to make use of the custom build feature which will significantly improve performance once your application is ready.
As a general tip on DHTML programming, use firebug (a plug-in for Firefox). It allows JavaScript debugging, DOM inspection, HTML editing in real-time and a whole lot more. I've become totally reliant on it now when I'm working in DHTML!
Good luck!
I too just dove head first into Dojo, they have a good API documentation at http://api.dojotoolkit.org/. Even Dojo Campus has some good examples of the plug ins.
If you ask me O'Reilly's Dojo: The Definitive Guide is the best Dojo book on the market.
I also would like any tips and pointers from the Dojo masters.
Cheers
Make sure documentation you read pertains to as recent a release as possible, since a lot has changed very quickly in the Dojo architecture.
Also a great way to see how some Dojo or Dijit widget is used is to look at the source code for the tests - for example, the DataGrid has poor documentation but the tests show a lot of use cases and configurations.
Sitepen is a good resource for Dojo articles.
Also, read up on Deferred (andDeferredList), as well as hitch() - two extremely flexible and powerful features of Dojo. SitePen has a great article on demystifying Deferreds.
Check out plugd, a collection of Dojo extensions that make some things more convenient or adds some clever functionalities to the language. It's made by one of the core Dojo authors so it's rather reliable. It even brings some jQuery niceties into the framework.
Some more things: look into data stores, they're very useful and a much cleaner way to handle Ajax. DojoX has a lot of nice ones too, just remember that DojoX ranges in how well documented or how experimental the components are. Learn the differences between dojo.byId and dijit.byId, as well as the HTML attributes id versus jsId (again, Sitepen has an article).
A couple of things that caught me when I started writing widgets where:
[Understand what dojoAttachPoint, dojoAttachEvent, containerNode and widgitsInTemplate do][1]
have a firm grasp of closures,
Get your head around deferreds
understand ItemFileReadStore, ItemFileWriteStore and stores in general
You can look at stores like a ResultSet (sort of) as well you can data bind them to widgets.
With these major concepts you can start to put together some compelling applications.
Generally what I do is I build a JavaScript facade around my service calls and then I will scrub the response into a store by attaching the first callback in the facade, that call back converts the results into a store and then returns it. This allows me to not hard bind my services to Dojo constructs (so I can support mobile, etc.) while also retuning the data from the facade in a format that data aware widgets expect.
As well if you are doing Java service development you my want to look into JAX-RS. I started out using JSON-RPC which became JABS-ORB but after working with JAX-RS I prefer it, as it integrates well with JPA-EJB and JAXB.
First read how to configure Dojo in your application. Try to understand basic structure of Dojo like if we are writing dijit.form.Button or dijit/form/Button it means Button.js resides in dijit/form folder. Try to understand require, define, declare modules of Dojo. This is enough to start Dojo Toolkit.
Very important fact, indulge with your own sample project using Dojo.
I'm not looking so much for language-specific answers, just general models for implementing a plugin system (if you want to know, I'm using Python). I have my own idea (register callbacks, and that's about it), but I know others exist. What's normally used, and what else is reasonable?
What do you mean by a plugin system? Does Dependency Injection and IOC containers sounds like a good solution?
I mean, uh, well, a way to insert functionality into the base program without altering it. I didn't intend to define it when I set out. Dependency Injection doesn't look particularly suitable for what I'm doing, but I don't know much about them.
A simple plugin architecture can define a plugin interface with all the methods the plugin ought to implement. The plugin handles event from the application, and can use the application's standard code, model objects, etc. to get things done. Basically the same as an ASP.NET Form does, except that you're overriding rather than implementing.
Nobody taught me this part, and I'm no expert, but I feel: In general a plugin will be less stable than its application, so the application should always be in control and only give the plugin periodic opportunities to act. If a plugin can register an Observer, then calls to the delegate should be tried/caught.
There is a very good episode of Software Engineering Radio, which you may be interested in.
For future reference, I have reproduced here the "Rules for Enablers" (alternative link) given in the excellent Contributing to Eclipse by Erich Gamma, Kent Beck.
Invitation Rule - Whenever possible, let others contribute to your contributions.
Lazy Loading Rule - Contributions are only loaded when they are needed.
Safe Platform Rule - As the provider of an extension point, you must protect yourself against misbehavior on the part of extenders.
Fair Play Rule - All clients play by the same rules, even me.
Explicit Extension Rule - Declare explicitly where a platform can be extended.
Diversity Rule - Extension points accept multiple extensions.
Good Fences Rule - When passing control outside your code, protect yourself.
Explicit API Rule - separate the API from internals.
Stability Rule - Once you invite someone to contribute, don?t change the rules.
Defensive API Rule - Reveal only the API in which you are confident, but be prepared to reveal more API as clients ask for it.
In Python you can use the entry-point system provided by setuptools and pkg_resources. Each entry point should be a function that returns information about the plugin -- name, author, setup and teardown functions, etc.
How about abstract factory? Your base program defines how the abstract concepts interact with each other, but the caller has to provide the implementation.