Hi everyone!
I,m studying Ember, and I have a few questions to understand the basic principles. I believe that this concerns the key points. Let me show you a little code, which is the boilerplate for me with typical tasks.
There is an onclick functions, data recieving from JSON, change an attributes of html using the received data.
These are the tasks that I want to understand most of all, and I will be very grateful to you for any help!
Please tell me how to implement all of this in the spirit of Ember?
I need to get the same result that will be reused in several routes, that is, different JSON will be processed. Perhaps the code should be contained in the component, but I could be wrong.
I really like Ember, everything is in its place here, and I really would like to figure out in which parts of Ember it needs to be separated (for example, the hbs template, the route and other parts that are needed), so what where should be placed.
I find it difficult to explain, but I think you will understand me.
Great thanks in advance for any help!
Excuse me my french
Hi @Nar_Zantaria. There’s a lot going on here that makes it hard to focus on one piece at a time. I’ll start with a high level summary:
Make a new ember app with ember new my-app. All the other files I mention here will be inside that project. cd my-app and run ember s to start the development server. You will see the welcome message at http://localhost:4200.
Your json file isn’t really a json file, it’s a javascript file. The confusion between the two may give you trouble. You’re currently loading it as a <script> but that won’t work for a real json file. I’m going to assume we’re using a valid json file and I’ll put it into public/fixed-data.json:
The public folder is where you can put any files inside an Ember app that you want to publicly available, unchanged, in the final application.
I will assume we want to put this demo onto the “homepage” of our application, so I’m going to work in the application route, which is the very topmost / main route that always renders in an Ember application. (Later in a real application you will probably want to move this onto some other route, so you can have different routes display different things.)
To load the data, make a Route file named app/routes/application.js like this:
Then edit app/templates/application.hbs. You can remove the initial content that’s in there about <WelcomePage />. First, let’s just make sure our data is loaded correctly by printing it out:
{{#each-in model as |section value|}}
<h1>{{section}}</h1>
{{#each-in value as |name count|}}
<div>{{name}}={{count}}</div>
{{/each-in}}
{{/each-in}}
You should now see your data printed in the browser. The model here corresponds with the model in app/routes/application.js. It holds your data. The rest of that snippet is just iterating over both levels of your file and printing out the values.
Next, instead of just printing the data we’re going to pass it into a component (we didn’t make the component yet, that will be the next step). Add this to to app/templates/application.hbs:
<ColorDemo @data={{model}} />
Now we need to make the component. You can run ember generate component color-demo in the terminal to get the files created automatically, or you can make them by hand. Here is the template file (app/templates/components/color-demo.hbs), which I based on your original HTML: