I’m working on an Ember.js app and I’m confused about how manually declared views and controllers interact with the application state. Here’s what I’m doing:
I have a main edit view and controller set up for a specific route.
When the user is on this page, they can create new elements that need their own views and controllers.
I’m creating these new views and controllers manually, like this:
let newView = MyCustomView.create({
controller: MyCustomController.create()
}).append()
But I’m not sure how this relates to the current application state. Should there be a connection between these new components and the main edit view/controller? Do I need to tell the router about these new elements?
I’m worried I’m missing something important about how Ember manages these relationships. Any help would be great!
In Ember.js, manually creating views and controllers as you’ve described can lead to some tricky situations with application state management. While it’s possible to do this, it’s generally not recommended in modern Ember apps.
Instead, consider using components for these dynamically created elements. Components are self-contained and can manage their own state, making them ideal for this use case. You can render them dynamically using {{component}} helper or {{in-element}} modifier.
If you need to maintain state across these dynamic elements, you might want to look into using services or ember-data store. This way, your main controller can act as a coordinator, managing the overall state and passing it down to child components as needed.
Remember, Ember’s strength lies in its conventions. Straying too far from these can make your app harder to maintain and reason about in the long run.
I’ve been down this road before, and I can tell you from experience that manually creating views and controllers can lead to some headaches with state management. While it’s doable, it’s not the most Ember-friendly approach.
In my projects, I’ve found great success using components for dynamic content. They’re more modular and easier to reason about. For state management across these dynamic elements, I’ve leveraged services effectively. They provide a clean way to share data and functionality.
One trick I’ve used is to have the main controller act as a coordinator. It manages the overall state and passes relevant bits down to child components as needed. This approach has made my code more maintainable and easier to debug.
Remember, Ember shines when you stick close to its conventions. Stepping too far outside those lines can make things unnecessarily complex. Trust me, I learned that the hard way!
hey there! i’ve run into similar issues. one thing to consider is using components instead of manually creating views/controllers. they’re more self-contained and easier to manage. you could also look into services for sharing state across different parts of your app. hope that helps!