ContainerView: challenges encountered when dynamically inserting views

ContainerView.pushObject() does not automatically connect dynamically added views with a Container object. The absence of an auto-wired container leads to a rendering error when a view renders a template containing a handlebars render helper.

SIMPLE SCENARIO THAT FUNCTIONS (TO SOME EXTENT)

View:

App.DynamicView = Em.View.extend({
    templateName: 'dynamic',
    didInsertElement: function() {
        var control = this.get('controller');
        control.send( 'view_inserted',  this.templateName, control._debugContainerKey);
        control.send('callDynamicController');
    }
});

Template:

<script type="text/x-handlebars" data-template-name="dynamic">
    dynamic
</script>

Controller (only used when manually assigned):

App.DynamicController = Em.ObjectController.extend({
    className: 'App.DynamicWithRenderController',
        callDynamicController: function() {
            console.log('DynamicController.callDynamicController()');
    }
});

Index Controller:

App.IndexController = Em.ObjectController.extend({
    view_inserted: function(aview, acontroller) { 
        console.log('view inserted!', aview, acontroller);
    }
})

Instantiation code:

var acontainer = App.DynamicController.create({});
var aview = App.DynamicView.create({ controller: acontroller })
acontainerView.pushObject(aview);

These classes render & behave as expected, but if you interogate them, lack some of the Ember-wiring (e.g. no _debugContainerKey & container properties IIRC):

ADVANCED SCENARIO THAT FAILS

If we introduce a handlebars template that uses rendering helpers, it breaks rendering. The dynamically added view currently lacks some properties the rendering helper assumes

<script type="text/x-handlebars" data-template-name="dynamic-with-render">
    dynamic w/render:
    {{render knob}}
</script>

and make knob look like this:

<script type="text/x-handlebars" data-template-name="knob">
    &#123;&#123;render knob&#125;&#125;
</script>

The (failing) dynamic view instantiation code:

var acontainer = App.DynamicController.create({});
var aview = App.DynamicView.create({ 
    controller: acontroller, 
    template:'dynamic-with-render' })
acontainerView.pushObject(aview);

CODE EXAMPLE

A more comprehensive example with some notes can be seen here:

http://jsfiddle.net/AshCoolman/KyJ2U/6/embedded/result/

NOTE: My tests include a custom handlebars helper based on the control helper called controlWithVars

THE ISSUE

It seems like I need to develop something that handles the Ember-wiring, either through:

  1. the more native ContainerView (digging into the Ember internals), OR
  2. a more detached new render helper possibly in an inelegant manner)

I'm uncertain about the next steps. It would be helpful if someone has already found an elegant solution, or could provide some useful tips.

USEFUL RESOURCES

https://github.com/emberjs/ember.js/issues/2108

What is the purpose of the Ember.Container

Answer №1

The main purpose of Ember.ContainerView is to dynamically include and exclude views, giving you the flexibility to achieve your desired functionality.

When examining your examples, one key observation is that you are creating child views with View.create(attrs). It is crucial to utilize

containerView.createChildView(viewClassName, attrs)
for creating views that inherit the container, parent view hierarchy, and more. For a closer look at the implementation, refer to this link:

https://github.com/emberjs/ember.js/blob/master/packages/ember-views/lib/views/view.js#L2072

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to access Firebase data in Vue.js, with or without Vuefire, using a router parameter before the DOM is rendered?

When I navigate to a view from another view, I pass the itemId as a param value to vue router. My goal is to call firebase with that itemId in order to filter the data and use the filtered result/data in the UI. Specifically, I am utilizing vuefire. The ...

Using AJAX to add an event listener and passing parameters to a separate function

Short and sweet - I've got a loop that creates a series of dynamic buttons, and when one of them is clicked, I need to pass its id attribute to another file to generate a new page on the fly. Here's the code snippet: for (var i in data.contacts ...

Discover the method to retrieve HTML content by sending an HTTP POST request in AngularJS, similar to loading content

My attempt to retrieve HTML content using Angular Js Post HTTP method was successful, but only for text contents like P tags. I am now trying to fetch HTML contents that include input types such as text boxes using Angular JS. Interestingly, when I utilize ...

The issue I am facing is with the post_logout_redirect_uri not functioning properly when using localStorage in an OIDC Auth

authority: 'yyy', client_id: this.'yyy', redirect_uri: 'http://localhost:4200/login', response_type: 'token', scope: 'yyy', post_logout_redirect_uri: & ...

Strip away double quotation marks from object attributes except those beginning with a number

I've searched extensively and reviewed various Q&A forums, but I haven't encountered a scenario quite like this. Here's an example of the object I'm working with: props: { "label": "1rem", "text3": "1rem", "text2Button": "1re ...

Refreshing a web page in Internet Explorer can cause the HTTP Header content to be altered

As I monitor my dashboard retrieving continuous data from an SAP server, I stumbled upon a solution to fetch the current server date. This approach allows me to display when the dashboard was last updated, and this date is displayed in the DOM. //Method f ...

Executing a jQuery function only once per click on a select element - how can it be done?

I have a form with a select element, and I want some code to run when I click on it, but not when I choose an option. The issue is that the code is running twice, preventing me from selecting an option as it resets itself each time. Here is the HTML: &l ...

What is the best way to showcase the outcome on the current page?

This is a sample HTML code for a registration form: <html> <head></head> <body> <form id="myform" action="formdata.php" method="post"> username:<input type="text" name="username" id="name"><br> password:&l ...

The functionality of jQuery clone is failing when draggable items are used to generate a duplicated version

I'm new to implementing jQuery Draggable functionality and I'm trying to create a drag and drop feature where the drag element is cloned and dropped to a specific location. Here is the code I have so far: $(function () { $('#D ...

using hover/click functionality with a group of DIV elements

I have a group of DIV elements that I want to apply an effect to when hovering over them with the mouse. Additionally, when one of the DIVs is clicked, it should maintain the hover effect until another DIV is clicked. <div class="items" id="item1"> ...

Customizing the placeholder font size in Material UI Autocomplete using ReactJS

Is there a way to change the placeholder font size for Material UI Autocomplete? https://i.stack.imgur.com/x71k2.png <Autocomplete multiple id="tags-outlined" options={top100F ...

Can a wasm file be registered in a similar way to a JavaScript file?

My JavaScript file is calling a WebAssembly (wasm) file, but it is fetching the file from the wrong location when I register the script in a specific part of the code. To address this issue, what is considered the best practice? Here's the current s ...

"Adding content to the DOM using AJAX, the data is showing up as plain text rather than formatted

As part of my project, I am working on incorporating data retrieved through an AJAX request into the DOM to be able to manipulate it further. However, I encountered an issue where the data displayed as a string instead of HTML when appended to the DOM. Bel ...

One way to transfer data from the back end into a two-dimensional array is by retrieving the data and then transforming it into the desired format before sending it to

How can I format backend data into a specific format using JavaScript? Even though I retrieve data from the backend, json_encode($myarry) does not display my data. $query = "SELECT * FROM LOCATIONS"; $result= mysql_query($query); while($row = mysql_fetch ...

Simplest method for defining an associative array

Looking to create an array in the format shown below: [0: 0, 1: 1, 2: 2] This is achieved using the following code snippet: var arr = []; for(i=0; i<3; i++){ arr[i] = i; } Upon execution, my array appears as follows: [0, 1, 2] The values with ...

Two-column layout with consistent height vertically, but varying heights on individual rows

By using JavaScript, I am able to ensure that two columns have equal height. The left column contains user input which can vary in length, causing some columns to have more content than others. My current issue is that there are multiple rows instead of ju ...

Guide to sending an ajax request from one page and receiving the response on a different page

Looking for advice on sending Ajax requests using jQuery and HTML5. I have multiple pages within my application. Is it possible to send an Ajax request from one page (e.g sync.html) and receive a response on another page (e.g home.html)? I am aware of alte ...

In JavaScript, when a div element contains an iframe, the click event will not trigger on the div

Check out this sample HTML markup: <div> <iframe></iframe> </div> The iframe is set to fill the outer div. I've added an event handler to the div click event: div.addEventListener("click", function () { console.log( ...

Encountering the error message "Unable to access property 'addEventListener' of null while trying to manipulate innerHTML"

Currently, I am utilizing innerHTML to include certain elements and a wrapper around them. for (i = 0; i < arr.length; i++) { b.innerHTML += "<div class='wrapper'><div class='col-4'>" + arr[i].storeID + "</div> ...

Share JSON data across functions by calling a function

I am currently working on a project where I need to load JSON using a JavaScript function and then make the loaded JSON objects accessible to other functions in the same namespace. However, I have encountered some difficulties in achieving this. Even after ...