Is there a way to organize Meteor templates so it isn't hard to figure out what's the data context? - meteor

As my Meteor project grows and I add more templates, partials and helpers, it gets harder to figure out what will be the data context for it. Then I'll have to console.log(this) inside a helper function to figure out what's the data I'm dealing with.
Does anybody have a naming scheme or any other strategy to handle this?
Or is this mess just a sign I'm failing to modularize stuff properly and should refactor everything?

For me, each module has a folder. Each folder contains helpers.js, events.js, the "ons" (onCreated.js, onDestroyed.js, onRendered.js) and finally templates.html. If your project is big, break these out into individual subfolders for the necessary CRUD actions (I have a create folder and an update folder because reading & deleting happens in the update templates.
My template names are long and verbose, but that's OK, WebStorm does a good job of guessing what I want. For example, if I had some infowindow that listed all the addresses associated with a client: clientMap, clientMapPopup, clientMapPopupLocationList clientMapPopupLocationListItem.
Regarding data context, it's usually pretty easy to see since my helper is the one that added something to the context. Although I honestly try to avoid using that unless I'm in an {{#each}} because IMHO things like grabbing grandparent context is neither elegant nor robust. Instead, I have a temporary module object that I create & destroy on route changes.
So if my global object is Global = {}. In the onCreated and onDestroyed I write Global.Module = {} Then, I can create all the module-scoped variables I want (ReactiveVars, ReactiveDicts, local collections, primitives, client markers object, etc).
All that said, doing what you do & looking at my schema js (e.g. collections/clients.js) is still the fastest/thoughtless way to see what you want & what you're currently getting.

Related

Is it safe to initialize a struct containing a std::shared_ptr with std::memset?

I'm modifying a code written in c++ in order to add several features required by my company. I need to modify as less as possible this code, because it's a public code get from a Git repository, and we want to avoid to deviate from the original source code in case we need to synchronize our code with possible new versions in the future.
In this code, a structure is initialized with a call to std::memset. And I had need to add a shared pointer to this structure.
I notice no issue about that, the code compiles, links and works as expected, and I get even no warnings while the compilation.
But is it safe to achieve that this way? May a std::shared_ptr be correctly initialized if it is part of a structure initialized with std::memset? Or are side effects or hazardous issues which prevent to do that?

Benefits of using pyuic vs uic.loadUi

I am currently working with python and Qt which is kind of new for me coming from the C++ version and I realised that in the oficial documentation it says that an UI file can be loaded both from .ui or creating a python class and transforming the file into .py file.
I get the benefits of using .ui it is dynamically loaded so no need to transform it into python file with every change but what are the benefits of doing that?, Do you get any improvements in run time? Is it something else?
Thanks
Well, this question is dangerously near to the "Opinion-based" flag, but it's also a common one and I believe it deserves at least a partial answer.
Conceptually, both using the pyuic approach and the uic.loadUi() method are the same and behave in very similar ways, but with some slight differencies.
To better explain all this, I'll use the documentation about using Designer as a reference.
pyuic approach, or the "python object" method
This is probably the most popular method, especially amongst beginners. What it does is to create a python object that is used to create the ui and, if used following the "single inheritance" approach, it also behaves as an "interface" to the ui itself, since the ui object its instance creates has all widgets available as its attributes: if you create a push button, it will be available as ui.pushButton, the first label will be ui.label and so on.
In the first example of the documentation linked above, that ui object is stand-alone; that's a very basic example (I believe it was given just to demonstrate its usage, since it wouldn't provide a lot of interaction besides the connections created within Designer) and is not very useful, but it's very similar to the single inheritance method: the button would be self.ui.pushButton, etc.
IF the "multiple inheritance" method is used, the ui object will coincide with the widget subclass. In that case, the button will be self.pushButton, the label self.label, etc.
This is very important from the python point of view, because it means that those attribute names will overwrite any other instance attribute that will use the same name: if you have a function named "saveFile" and you name the button "saveFile", you won't have any [direct] access to that instance method any more as soon as setupUi is returned. In this case, using the single inheritance method might be helpful - but, in reality, you could just be more careful about function and object names.
Finally, if you don't know what the pyuic generated file does and what's it for, you might be inclined to use it to create your program. That is wrong for a lot of reasons, but, most importantly, because you might certainly realize at some point that you have to edit your ui, and merging the new changes with your modified code is clearly a PITA you don't want to face.
I recently answered a related question, trying to explain what happens when setupUi() is called in much more depth.
Using uic.loadUi
I'd say that this is a more "modular" approach, mostly because it's much more direct: as already pointed out in the question, you don't have to constantly regenerate the ui files each time they're modified.
But, there's a catch.
First of all: obviously the loading, parsing and building of an UI from an XML file is not as fast as creating the ui directly from code (which is exactly what the pyuic file does within setupUi()).
Then, there is at least one relatively small bug about layout contents margins: when using loadUi, the default system/form margins might be completely ignored and set to 0 if not explicitly set. There is a workaround about that, explained in Size of verticalLayout is different in Qt Designer and PyQt program (thanks to eyllanesc).
A comparison
pyuic approach
Pros:
it's faster; in a very simple test with a hundred buttons and a tablewidget with more than 1200 items I measured the following bests:
pyuic loading: 33.2ms
loadUi loading: 51.8ms
this ratio is obviously not linear for a multitude of reasons, but you can get the idea
if used with the single inheritance method, it can prevent accidental instance attribute overwritings, and it also means a more "contained" object structure
using python imports ensures a more coherent project structure, especially in the deployment process (having non-python files is a common source of problems)
the contents of those files are actually instructive, especially for beginners
Cons:
you always must remember to regenerate the python files everytime you update an ui; we all know how easy is to forget an apparently meaningless step like this might be, expecially after hours of coding: I've seen plenty of situations for which people was banging heads on desks (hopefully both theirs) for hours because of untraceable issues, before realizing that they just forgot to run pyuic or didn't run it on the right files; my own forehead still hurts ;-)
file tracking: you have to count two files for each ui, and you might forget one of them along the way when migrating/forking/etc, and if you forgot an ui file it possibly means that you have to recreate it completely from scratch
n00b alert: beginners are commonly led to think that the generated python file is the one to be used to create their programs, which is obviously wrong; unfortunately, the # WARNING! message is not clear enough (I've been almost begging the head PyQt developer about this); while this is obviously not an actual problem of this approach, right now it results in being so
some of the contents of a pyuic generated files are usually unnecessary (most importantly, the object name, which is used only for specific cases), and that's pretty obvious, since it's automatically generated ("you might need that, so better safe than sorry"); also, related to the issue above, people might be led to think that everything pyuic creates is actually needed for a GUI, resulting in unnecessary code that decreases its readability
loadUi method
Pros:
it's direct and immediate: you edit your ui on Designer, you save it (or, at least, you remember to do it...), and when you run your code it's already there; no fuss, no muss, and desks/foreheads are safe(r)
file tracking and deployment: it's just one file per ui, you can put all those ui files in a separate folder, you don't have to do anything else and you don't risk to forget something on the way
direct access to widgets (but this can be achieved using the multiple inheritance approach also)
Cons:
the layout issue mentioned above
possible instance attribute overwriting and no "ui" object "containment"
slightly slower loading
path and deployment: loading is done using os relative paths and system separators, so if you put the ui in a directory different from the py file that loads that .ui you'll have to consider that; also, some package managers use to compress everything, resulting in access errors unless paths are correctly managed
In my opinion, all considering, the loadUi method is usually the better choice. It doesn't distract me, it allows better conceptual compartmentation (which is usually good and also follows a pattern similar to MVC much more closely, conceptually speaking) and I strongly believe it as being far less prone to programmer errors, for a multitude of reasons.
But that's clearly a matter of choice.
We should also and always remember that, like every other choice we do, using ui files is an option.
There is people who completely avoids them (as there is people who uses them literally for anything), but, like everything, it all and always depends on the context.
A big benefit of using pyuic is that code autocompletion will work.
This can make programming much easier and faster.
Then there's the fact that everything loads faster.
pyuic6-Tool can be used to automate the call of pyuic6 when the application is run and only convert .ui files when they change.
It's a little bit longer to set up than just using uic.loadUi but the autocompletion is well worth it if you use something like PyCharm.

How to create an dynamic input list without creating a db collection

I'm trying to build a form in which you can dynamically add text inputs as required. I don't want to save anything until the person clicks 'save' so it's important that this is done without a db collection.
I came up with this solution (http://meteorpad.com/pad/zP8EGjigXASfFrXsF/Input%20Test) but I'm unsure if this is the correct approach or is there an easier way?
Most users probably don't need to directly use Tracker.Dependency anymore because there are now higher-level options that are are a bit easier to use. Here are two choices:
Client Collections
You can declare a client-side collection like:
InputOptions = new Mongo.Collection(null);
It will have all of the same behavior as a normal collection without trying to sync its data to the server. This is probably what you want. The only disadvantage is that the collection will be available to your whole application, so its reactivity is not isolated to a single template.
ReactiveVar
You can use either a ReactiveVar or a ReactiveDict and scope it to your template. This is a bit better than directly using Tracker.Dependency because you don't have to call changed all over the place. Overall the syntax is more cumbersome that a client-side collection, but you get the advantage of isolating the reactivity if you need more than one of them.

How to handle changes in objects' structure in automated testing?

I’m curious to know how feasible it is to get away from the dependency onto the application’s internal structure when you create an automated test case. Or you may need to rewrite the test case when a developer modifies a part of the code for a bug fix, etc.
We could write several automated test cases based on the applications internal object structure, but lets assume that the object hierarchy changes after 6 months or so, how do we approach these kind of issues?
I can't speak for other testing tools but at least in QTP's case the testing tool introduces a level of abstraction over the application so that non-functional changes in the application often (but not always) have no effect on the way the testing tool identifies the object.
For example in QTP all web elements are considered to be direct children of the document so that changes in the DOM (such as additional tables) don't change the object's description.
In TestComplete, there are a couple of ways to make sure that the changed app structure does not break you tests.
You can set up the Aliases tree of the Name Mapping feature. In this case, if the app structure is changed, you need to modify the Aliases tree appropriately and your test will stay working without requirement to modify them.
You can use the Extended Find feature of the Name Mapping in order to ignore parts of the the actual object tree and search for a needed objects on deeper levels.
This is what I was forced to do after losing all my work twice due to changes on the DOM structure:
Every single time I need to work with an object, I use the Find function with the ID of the object, searching for the object on the Page object. This way, whenever the DOM gets updated, my tests still run smoothly.
The only thing that will break my tests is if the object's ID get changed, but that's not very probable to happen.
Here you can find some examples of the helper functions I use.

Using a collection that already exists in Meteor

How do you access a Meteor collection that already exists? It's easy enough when you have created the collection in the session because you have a variable that references it, but you can't access a collection by name.
What happens if for example you want to retrieve documents from an existing collection in a new session where it is not being created for the first time. I have tried to 're-create' it hoping that it would just assign the existing collection to the new variable name (seeing that I can't find it by name), but it just throws an error to tell you that the collection already exists.
I've got some externally generated collections that I'm accessing via meteor. I'm not 100% sure that this will answer your question, but I hope it'll at least help.
One gotcha (doesn't apply to you it sounds like, here for completeness) is that if your collection was not created by Meteor, you'll need to export an environment variable to point Meteor to your DB.
For example, if the following env. variable is exported in the shell:
MONGO_URL=mongodb://localhost:3002/foo
...and then you invoke the meteor application, it'll point to the db "foo" in MongoDb, at which point you simply defined your collections as #Akshat mentions above in his comment:
collection = new Meteor.Collection("fooCollection") // this lives inside the foo DB.
If you're dealing with collections that have already been created by Meteor, by default they'll be inside the meteor db, eg:
MONGO_URL=mongodb://localhost:3002/meteor
...and you should be able to simply hook into them the same way; by simply declaring your collection and using it as you would. No need to create, obviously.
It sounds like you're already doing this but for other newcomers like me: in cases like this it's really handy to use the console in Chrome, Firefox, etc. and do some inserts that way - you'll see where your data lands, or you'll see other good bits of information that will help you home in on the issue - console.log() has saved my bacon a couple of times.
At any rate, it's worth validating exactly where the Meteor app is pointing vs. where you think it's pointing. Your collections should be accessible and should Just Work...

Resources