admin管理员组

文章数量:1293149

How in the world do i get a handle on the store inside of a ponent? I'm trying to create an auto-plete ponent that returns results from the store.

App.AutoCompleteComponent = Ember.Component.extend({

    //-------------------------------------------
    // Ember Properites
    //-------------------------------------------
    content: Ember.ArrayController.create(),

    //-------------------------------------------
    // Instance Properties
    //-------------------------------------------
    queryText: "",

    ponentItemSelected: null,

    //-------------------------------------------
    // Observers
    //-------------------------------------------
    queryTextChanged: function () {
        this.updateContent(this.get("queryText"));
    }.observes("queryText"),

    //-------------------------------------------
    // Instance Methods
    //-------------------------------------------
    selectItem: function (item) {
        this.set("ponentItemSelected", item);
    },

    updateContent: function (queryText) {

        if (queryText.length <= 5) {

            console.log('not greater than 5 chars');
            return;
        }

        this.get("content").setObjects([]);

        var items = App.Company.find();

        this.get("content").setObjects(items);
    }
});

here is my pany model

App.Company = DS.Model.extend({

  name: DS.attr('string'),

  created_at: DS.attr('date'),

  updated_at: DS.attr('date'),

  people: DS.hasMany('person')

});

I've tried:

  • this.get('store')
  • DS.Store.find('pany')
  • just store
  • App.Company.find()

I always get an Uncaught TypeError ... has no method 'find'

How in the world do i get a handle on the store inside of a ponent? I'm trying to create an auto-plete ponent that returns results from the store.

App.AutoCompleteComponent = Ember.Component.extend({

    //-------------------------------------------
    // Ember Properites
    //-------------------------------------------
    content: Ember.ArrayController.create(),

    //-------------------------------------------
    // Instance Properties
    //-------------------------------------------
    queryText: "",

    ponentItemSelected: null,

    //-------------------------------------------
    // Observers
    //-------------------------------------------
    queryTextChanged: function () {
        this.updateContent(this.get("queryText"));
    }.observes("queryText"),

    //-------------------------------------------
    // Instance Methods
    //-------------------------------------------
    selectItem: function (item) {
        this.set("ponentItemSelected", item);
    },

    updateContent: function (queryText) {

        if (queryText.length <= 5) {

            console.log('not greater than 5 chars');
            return;
        }

        this.get("content").setObjects([]);

        var items = App.Company.find();

        this.get("content").setObjects(items);
    }
});

here is my pany model

App.Company = DS.Model.extend({

  name: DS.attr('string'),

  created_at: DS.attr('date'),

  updated_at: DS.attr('date'),

  people: DS.hasMany('person')

});

I've tried:

  • this.get('store')
  • DS.Store.find('pany')
  • just store
  • App.Company.find()

I always get an Uncaught TypeError ... has no method 'find'

Share edited Aug 4, 2014 at 2:29 Kingpin2k 47.4k10 gold badges80 silver badges96 bronze badges asked Nov 14, 2013 at 3:34 DavidDavid 10.8k19 gold badges74 silver badges125 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

The real answer is you shouldn't. A ponent should be agnostic of the outside world, and adding a dependency on the store breaks that concept. Really you should get the models beforehand (in the route, or controller, depending on the logic) and passing them into the ponent.

https://github./emberjs/data/blob/master/TRANSITION.md

In general, looking up models directly in a ponent is an anti-pattern, and you should prefer to pass in any model you need in the template that included the ponent.

Now that I've said that, just pass the store into the ponent. It lives on the routes and controllers, so when you create a ponent send in the store as one of the arguments, then you can access it using this.get('store')

{{auto-plete store=controller.store}}

or

{{auto-plete store=store}}

http://emberjs.jsbin./OxIDiVU/720/edit

本文标签: javascripthow to get the store in a component in emberjsStack Overflow