mardi 27 octobre 2015

Ember.js Asynchronous Object Parameter

I am having some issues with Ember.js and loading resources asynchronously (aka I don't know what I'm doing). This is what I have. It currently doesn't update the list parameter after receiving the data. The template does not render anything. Thanks for any help.

Utilities

import Ember from 'ember';

export var page = Ember.Object.extend({
  type: null,

  list: Ember.computed('type', function() {
    var type = this.get('type');
    var url = "/test/" + type + "/test2";

    if (type) {
      getArray(url).then(function(list) {
        return list;
      });
    } else {
      return [];
    }
  })
});

export function get(url) {
  return Ember.$.ajax({
    url: url,
    type: 'GET',
        dataType: 'text',
        cache: true,
    xhrFields: {
         withCredentials: true
    }
  });
}

export function getArray(url) {
  return get(url).then(
    function(file) {
      var array = file.split("\n");
      array.pop();
      return array;
    },
    function() {
      return ["Error!"];
    }
  );
}

Route

import util from 'app/utils/utilities';

export default Ember.Route.extend({
  model(params) {
    var p = util.page.create({
      type: params.log_type
    });

    return p;
  }
});

Template

{{#each model.list as |item|}}
  <li><a href="{{item}}">{{item}}</a></li>
{{/each}}




Aucun commentaire:

Enregistrer un commentaire