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

Building a promotional widget

I'm currently working on developing an ad widget that can be easily reused. I'm debating whether to use jQuery or stick with pure JavaScript for this project. What are your thoughts on the best approach for creating a versatile and efficient ad w ...

Can PHP encode the "undefined" value using json_encode?

How can I encode a variable to have the value of undefined, like in the JavaScript keyword undefined? When I searched online, all I found were results about errors in PHP scripts due to the function json_encode being undefined. Is there a way to represent ...

Storing extensive JSON data with AJAX, jQuery, and Java

Currently, I am utilizing jQuery AJAX to call a server-side method and sending a JSON string to the controller. Everything works smoothly when the JSON size is small, but as soon as it exceeds 7kb, the server side rejects the JSON string. I suspect that t ...

What measures can be taken to ensure that the input checkbox is not toggled by accidentally pressing

There's a checkbox input in a dropdown menu at the top of the page that is giving me some trouble. When I select an option by clicking on it, for some reason pressing the spacebar toggles it off and on. Clicking outside once doesn't correct it, I ...

"Enhance search functionality by integrating live search results with clickable hyperlinks

I am currently working on implementing a live search feature in Laravel 7. Everything is functioning correctly, however, I am facing an issue where I am unable to add a (href a tag) to the result list. The results are being displayed in a select option usi ...

How to import an HTML file using TypeScript

I need to load an html file located in the same directory as the typescript file and return it from the function. public ...(... ) : angular.IHttpPromise<string> { ... return $http({ method: 'GET', url: &apos ...

Receiving a reply but failing to give a response

My code is causing an ERR_HTTP_HEADERS_SENT error. Can you help me identify what's wrong with it? .then((userDetails: any) => { userDetails.role.forEach((singleRole) => { if (singleRole.name === 'admin&apos ...

How can you integrate Dygraph into your React project alongside redux?

Lately, I've been facing some challenges while trying to integrate Dygraph into React (utilizing Redux). The Dygraph wrapper packages available on NPM don't seem to cooperate. Furthermore, the conventional method of using: <div id="graph"> ...

Ways to Loop Through a Set of Information and Retrieve it as a List

Here is some sample data: 0: {rowid: "4b531532a5a9", groups: "Group1", descriptions: "Item1"......} 1: {rowid: "e55315ccabb5", groups: "Group2", descriptions: "Item2"......} 2: {rowid: "f2713 ...

Having trouble executing the .map function within the function

Context In the process of developing a React-Redux application, I am faced with the task of handling axios calls to an external API over which I have no control. The specific axios request in question is executed by a function called "getData". This reque ...

Adding an arrow to a Material UI popover similar to a Tooltip

Can an Arrow be added to the Popover similar to the one in the ToolTip? https://i.stack.imgur.com/syWfg.png https://i.stack.imgur.com/4vBpC.png Is it possible to include an Arrow in the design of the Popover? ...

Problem encountered with AngularJS html5mode URL functionality

I am encountering an issue with my AngularJS application that does not contain any nodeJS code. The problem lies in removing the # from the URL and I have implemented ui-routes for routing. 'use strict'; var app = angular.module('myapp&apos ...

I'm a complete programming newbie and I want to start learning JavaScript, jQuery, and other programming languages. Where should I

Coming from a design background with zero programming knowledge, I have recently learned XHTML and CSS. Now, I am eager to expand my skills by mastering JavaScript, jQuery, and more. Where should I begin? This will be my first foray into programming. Whil ...

Attempting to transfer a newly created element from one location to another within the document after it has finished

I'm currently updating the design of my website. The currency selector app by Shopify is placed at the bottom, which has caused confusion for my international customers. To resolve this issue, I want to move it to a specific div class called .CSPositi ...

Tips for customizing the cursor to avoid it reverting to the conventional scroll cursor when using the middle click

When you wish to navigate by scrolling with the middle mouse button instead of using the scroll wheel, simply middle-click and your cursor will change allowing for easy scrolling on web pages. Even though I am utilizing style="cursor: pointer" and @click. ...

Execute function periodically using Vue.js

I have a requirement to update data periodically by calling a function in my Vue.js application. I am looking to call the 'listar()' method every 30 seconds and store the response in a property of vue js. If anyone can guide me on where to locat ...

Using asynchronous functions in JavaScript to push an array does not function correctly

Trying to update the users array by pushing the dirs, but for some reason the dir property remains blank in the console.log statement after this. Any suggestions? I know I could use a synchronous function, but a friend mentioned that synchronous functions ...

Discovering an item within an array of objects using the find method in Ajax

Within my backend PHP code, I iteratively define the following: foreach ( $data as $res ){ $approved[ ] = [ 'id' => $count, 'title' => "some title" ]; $count++;$title=... ) This eventually leads to the creation ...

URL Labeler StateProvider: A unique StateProvider that adds a distinctive label to the

I need help figuring out how to add a user-friendly URL at the end of the current URL using $stateProvider. As an example, on StackOverflow's question pages, the URL format is stackoverflow.com/questions/(question_id)/(question title). I want to repl ...

Issue with triggering jQuery .submit() function on Bootstrap 3 modal form

I've been attempting to use a Bootstrap 3 modal form to trigger a form.submit() in jQuery, but despite my efforts, I can't seem to get it to work as intended. <div class="modal fade" id="modal-signup" tabindex="-1" role="dialog" aria-labelled ...