Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7987507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:10:09+00:00 2026-06-04T12:10:09+00:00

Goal To create an array of Model’s, managed by an ArrayController (ArrayProxy). Requirements Use

  • 0

Goal

To create an array of Model’s, managed by an ArrayController (ArrayProxy).
Requirements

Use ArrayController (ArrayProxy) abstraction to encapsulate the array of Model’s
Convert objects input to ArrayProxy automagically upon insertion into the ArrayProxy
Do not lazy convert at access time

Example Data Structures

App.AddressModel = Ember.Object.extend({
    address_name: null,
    address: null,
    printme: function() {
        console.log("Just making sure the array turned into an AddressModel");
    },
});

App.addressArray = Ember.ArrayProxy.create({
    transformFrom: function(item) {
    },
    transformTo: function(item) {
    },
    arrayContentWillChange: function(startIdx, removeAmt, addAmt) {
    },
});

Failures by Trial

dynamic property

Someone in the IRC channel mentioned trying dynamic properties. This resulted in a, what appeared to be by logic and empirical evidence, a recursive result. No doubt from making content both the dynamically generated variable and the ‘trigger/exported’ variable.

arrayContentWillChange

Again, a seemingly recursive result. Upon receiving a arrayContentWillChange notification I generate an AddressModel from the given array index item(s). I then set the old indexed item to the created Model and an arrayContentWillChange event is triggered again, repeat … recurse.

transformFrom / transformTo

https://github.com/emberjs/ember.js/pull/554#issuecomment-5401112
tomdale mentions in the above post to try using transformFrom and transformTo to cast the incoming and/or outgoing data. These function don’t seem to exist [http://cloud.github.com/downloads/emberjs/ember.js/ember-0.9.8.js].

ArrayProxy model patch

https://github.com/emberjs/ember.js/pull/554
tomdale’s suggestion to the original solution/post to this problem seems to generalize better than the model implementation introduced by jedwood, however, in Backbone.js handles this problem by using the special model variable and I found it to work well.

The Question

How do I extend ArrayProxy to convert all incoming object, to be managed, into an AddressModel?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:10:11+00:00Added an answer on June 4, 2026 at 12:10 pm

    I took the approach mentioned by Tom Dale which overwrites the replace method. It also registers an observer for the content property to assure that the content is a typed version of the array, see http://jsfiddle.net/pangratz666/XCLmE/:

    App.ModelArrayProxy = Ember.ArrayProxy.extend({
        modelType: Ember.required,
    
        _typedArray: function(objects) {
            var modelType = this.get('modelType');
            var array = objects.map(function(item) {
                // check if item is an instance of type
                if (!(item instanceof modelType)) {
                    // create a new instance of type and pass the hash
                    return modelType.create(item);
                }
    
                // just return the item, since it's already of type modelType             
                return item;
            });
            return array;
        },
    
        contentChanged: function() {
            if (!this.get('setTypedContent')) {
                var content = this.get('content');
    
                this.set('setTypedContent', true);
                this.set('content', this._typedArray(content));
                this.set('setTypedContent', false);
            }
        }.observes('content'),
    
        replace: function(idx, amt, objects) {
            this._super(idx, amt, this._typedArray(objects));
        }
    });
    

    This ModelArrayProxy can then be used as a “normal” ArrayProxy:

    // model declaration
    App.AddressModel = Ember.Object.extend({
        ...
    });
    
    // create a proxy for given modelType
    App.proxy = App.ModelArrayProxy.create({
        content: [],
        modelType: App.AddressModel
    });
    
    var myArray = [{
        address_name: 'address_name 1',
        address: 'address 1'},
    {
        address_name: 'address_name 2',
        address: 'address 2'}];
    
    // set content with "normal" objects
    App.proxy.set('content', myArray);
    
    // invoke 'log' on all items in array
    App.proxy.forEach(function(item) {
        item.log();
    });
    
    // push a normal object
    App.proxy.pushObject({
        address_name: 'address_name 3',
        address: 'address 3'
    });
    
    // push an instance of App.AddressModel
    App.proxy.pushObject(App.AddressModel.create({
        address_name: 'address_name 4',
        address: 'address 4'
    
    }));
    
    // invoke 'log' on all items in array
    App.proxy.forEach(function(item) {
        item.log();
    });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Goal To use a CREATE TYPE statement in HSQLDB 2.0.0 to create a user-defined
My goal here is to create an array with the sum totals of every
Duplicate: PHP validation/regex for URL My goal is create a PHP regex for website
my goal is to create a sort of Javascript library, if you could call
The goal is to create a mock class which behaves like a db resultset.
My goal is to create an efficient structure to store the most relevant entries
My goal is to create something like 1Up.com has where website viewers gain Experience
My Goal: i want to create an horizontal list of images. when mouse is
Im trying to create a moving back ground. My goal is to have a
How do I create an exclusion for a array map in Ruby. Here's what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.