Is there a way to pass parameters when creating a Enyo app class? - enyo

I was going to try in pass in parameters when I create my Enyo app class in the index.html file. I have the following to test it
new MyApps.MainApp("test").renderInto(document.body);
and in the js file
create: function(in)
{
Alert(in);
}
Is there a way to do this?

You're really, really close. If you want to set some variables on your app's kind then you need to pass the parameters as you would to any other kind. Try:
new MyApps.MainApp({test: true}).renderInto(document.body);
Then, you should be able to access the value of test as: this.test
Hope that helps.

new MyApps.MainApp({test: true}).renderInto(document.body);
...
enyo.kind({
name: "MyApps.MainApp",
kind: enyo.VFlexBox,
components: [],
create: function(inArgs) {
var args = inArgs;
if (args.test) {
this.log("SUCCESS");
}
this.inherited(arguments);
}
});
Something like that.

Related

How do I handle values that are dependent on the result of a helper?

So I have an Angular controller with a meteor helper method, as below.
function localeCtrl($scope, $reactive, $stateParams{
$reactive(this).attach($scope);
var self = this;
self.helpers({
locale: function(){ return Locales.findOne($stateParams.id)},
staff: function(){
// Load data from second collection based on current Locale.
// But how?
},
address: function(){
// Take self.location.address and massage it to provide
// google maps link. How?
}
tags: function(){
// Collect all unique instances of a given tag by
// iterating over the available locales.
// E. G. If 10 locales have the 'restaurant' tag, and 5
// more have the 'library' tag, I want an array of
// ['restaurant', 'library'] -- easy enough to do
// by iterating over the locales, but how do I do that
// reactively?
}
});
}
Unfortunately, I need to set additional properties based on the data fetched by locale(). I can't set them up when I initialize the controller because the value in locale() changes as data is fetched from the server. But I need access to the data in locale to, for example, create the google maps address, or fetch associated records. (They aren't imbedded in the locale document for reasons that I'm sure made sense at the time).
Edit:
Additionally, I'm using ground DB to store a local copy of the data for offline access, which makes life even more complicated.
Probably you best bet is to publish your collection using publishComposite which is implemented using the reywood:publish-composite package.
Add the package:
meteor add reywood:publish-composite
Now where you publish the Locales collection you would do something like this:
Meteor.publishComposite('locales', function() {
return {
find() {
//Put whatever you need in the query for locales
const query = {
_userId: this.userId
};
return Locales.find(query);
},
children: [{
find(locale) {
return Staff.find({ localeId: locale._id });
}
}]
};
});
Then in your controller before the helper you add this:
this.subscribe('locales');
Now you should be able to simply call the code like this:
this.helpers({
locale(){
return Locales.findOne(this.$stateParams.id);
}
});
And access it in the template like this:
locale.staff
Give that a try and let me know!

Setting up global variable in SPA (Hot Towel)

I want to have a global variable that is accessible to modify in any view in my application. I am wondering what this the best way to do so when using hot towel. Currently, I am declareing this variable in my shell.js and referencing this in subsequent views.
My shell.js returns the global variable. My other views have the shell defined at the top like define(['viewmodels/shell'], function(shell){} and I access the shell variable like shell.variablename. I am wondering if this is the best way of handling variables that need to be accessed by more than one view.
It can be done in many ways , either u create a config.js or any file or u can include the following code inside 'vm' object.
// config.js
define(function () {
var variableName= 'BlahBlah';
return {
debugEnabled: ko.observable(true),
variableName :variableName
};
});
// include this config.js in any javascript file
define(['folderName/config'],
function (config) {
var x= config.variableName;
}
This will work

How to use URL parameters using Meteorjs

How can I use URL parameters with meteor.
The URL could look like this: http://my-meteor.example.com:3000?task_name=abcd1234
I want to use the 'task_name' (abcd1234) in the mongodb query in the meteor app.
eg.
Template.task_app.tasks = function () {
return Tasks.find({task_name: task_name});
};
Thanks.
You are probably going to want to use a router to take care of paths and rendering certain templates for different paths. The iron-router package is the best one available for that. If you aren't using it already I would highly recommend it.
Once you are using iron-router, getting the query strings and url parameters is made very simple. You can see the section of the documentation here: https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#route-parameters
For the example you gave the route would look something like this:
Router.map(function () {
this.route('home', {
path: '/',
template: 'task_app'
data: function () {
// the data function is an example where this.params is available
// we can access params using this.params
// see the below paths that would match this route
var params = this.params;
// we can access query string params using this.params.query
var queryStringParams = this.params.query;
// query params are added to the 'query' object on this.params.
// given a browser path of: '/?task_name=abcd1234
// this.params.query.task_name => 'abcd1234'
return Tasks.findOne({task_name: this.params.query.task_name});
}
});
});
This would create a route which would render the 'task_app' template with a data context of the first task which matches the task name.
You can also access the url parameters and other route information from template helpers or other functions using Router.current() to get the current route. So for example in a helper you might use Router.current().params.query.task_name to get the current task name. Router.current() is a reactive elements so if it is used within the reactive computation the computation will re-run when any changes are made to the route.

How to define routes that include the current user with Iron Router?

I'm fairly new to Meteor and Iron Router and I'm trying to create a route that looks like this '/users/hippomoe/bookmarks/Kj6ecNk7DeW7PmmGA' where hippomoe would be the current logged in user and Kj6ecNk7DeW7PmmGA would be the id of a bookmark. Some of the things that I've tried were to add a handlebars helper that would provide the username
bookmark: function() {
return {
_id: this._id,
user: Meteor.user().username
};
}
and I also tried defining user: Meteor.user().username in the data context within the RouteController definition. In both instances I get the following error: "You called Route.prototype.resolve with a missing parameter. "user" not found in params"
I've tried finding an example that illustrates this type of route (which I assume would be common). The other questions that I have seen on StackOverflow related to this were about getting the user associated with a particular document and not the current user that is logged in.
It seems that I missing something simple. Can someone provide/point me to a simple example of how to achieve this?
I'm having difficulty understanding why you would need such a route. If you already know the name of the logged in user, why would you need to have it once more in the route? Or is this meant to be server-side? In that case, please show your routes.
Otherwise, does this not work:
Router.map(function () {
this.route('postShow', {
path: '/users/:ignoreme/bookmarks/_id',
data: function () {
var params = this.params;
var user = Meteor.user().username;
if (user == params.ignoreme) {
...
} else {
return "something went wrong";
}
}
});
});
This is just to allow the url pattern you want, but again, I don't see why you wouldn't just use a URL /user/bookmarks or similar and then change the logic to depend on Meteor.user().
The handlebars helper that I had written in my question
bookmark: function() {
return {
_id: this._id,
user: Meteor.user().username
};
}
would have worked if I would have specified the pathFor syntax correctly. My pathFor should have looked like this
{{pathFor 'saveBookmark' bookmark}}
I had inadvertently left off bookmark!
I think you are missing a ':' before _id, try this:
path: '/users/:ignoreme/bookmarks/:_id',

change collection before publishing

I would like to add a property to the objects that get published to the client.
My publish function looks like that
Meteor.publish("forms", function() {
return Forms.find();
});
I would like to do something like this
Meteor.publish("forms", function() {
var forms = Forms.find();
forms.forEach(function (form) {
form.nbForms = 12;
}
return forms;
});
What I would like is that all the documents in forms have a new count attribute which gets sent to the client.
But this obviously does not work.
thank you for your help
Not sure it will work in your case but you might use the new transform collection function introduced with Meteor 0.5.8
When declaring your collection, add this function as the second parameter :
Forms = new Meteor.Collection("forms", {
transform: function(f) {
f.nbForms = 12;
return f;
}
});
But this will be on both server and client. I don't know if there is a way to define a transform function in a publish context.
I think you need to do something similar to this Meteor counting example in Publish:
How does the messages-count example in Meteor docs work?
I also posted a question here that may help once it's answered. Meteor has a this.added which may work, but I'm currently uncertain how to use it. Hence the question below:
Meteor, One to Many Relationship & add field only to client side collection in Publish?

Resources