Is there a way to put a function in something like main.js and then in app.js call it properly?
For an instance I have this in app.js right now…
App = Ember.Application.create();
App.Router.map(function() {
// put your routes here
});
App.IndexRoute = Ember.Route.extend({
actions: {
newIngredient: function() {
newIngredient();
}
}
});
App.IndexController = Ember.ObjectController.extend({
actions: {
submit: function() {
alert('submited');
}
}
});
// New ingredient button
function newIngredient() {
$('<input class="ingredient-input" type="text" name="ingredient" /><br>').fadeIn().insertBefore($('#ingredient-btn'));
$('input:last').focus();
}
But I would like to move that function into a different file (main.js) to keep things organized (my type A coming out).
What is the best way to get this done, I’ve done some searching but being so new at ember still there are some things talked about that I either can’t follow or don’t feel like are really solving such a simple problem.
You can put that function anywhere as long as the script is included in HTML. As long as the function is available in global scope, you’ll be able to access it.
Looking at your code, you’re going to have a hard time doing what you’re doing. The Ember way of doing this would be to put that input field into a template and have the submit button trigger the event that should be handled in the route.
You might want to checkout my article on Ember Sherpa embersherpa.com it will give you a good overview of bunch of things that’s helpful to know when starting with Ember.
I’ve tried to figure out how to grab those inputs as a list of ingredients so I can use them with an API later.
EDIT: Just noticed the “JSON.stringify” at the bottom of that fiddle which was commented out and using that I can alert the contents but I’m still not sure how to grab those values.
My next article is going to be about preloading data from APIs, in the mean time, checkout this article.
Remember to convert your objects to Ember objects by with Em.Object.create(item), its not enough to just insert them into content array because the plain JavaScript objects won’t have Ember methods
If you want to see all the values just pass the whole object. console.log is a function that can take multiple values. You can mix and match text and values, even whole objects and arrays. For example
console.log("My data %O", data);
The capital O tells the console to print the whole object. If it was a string you could also do something like:
console.log("My string is %s", string_variable);
You could even pass in multiple values.
console.log("First string: %s Second string: %s", string1, string2);
Going back to your array you could even display arbitrary values:
console.log(data[0].ingredient);
console.log("First value: %s Second value: %s",
data[0].ingredient,
data[1].ingredient );
and so on.
More details on console.log here:
%s Formats the value as a string.
%d or %i Formats the value as an integer.
%f Formats the object as a floating point value.
%o Formats the value as an expandable DOM element (as in the Elements panel).
%O Formats the value as an expandable JavaScript object.
%c Applies CSS style rules to output string specified by the second parameter.