On page/app load create createRecord based on queryParams array

I’m entirely new to EmberJS. I’ve been fiddling around lately, but find my self in a bit of a jam with a specific issue. I’m still getting grips on the basic concepts of this framework, so bear with me.

Basically I want to manipulate the ember store (only locally) on page/app load. Specifically an array of queryParams should create a series of new records on page/app load.

The query could look like: http://localhost:4200/?post[]=1&post[]=2

I’m able to fetch the queryParams and loop over the desired query (here post) but as soon as i try to access the store from within the forEach loop a console error is thrown back at me: Cannot read property 'store' of undefined TypeError: Cannot read property 'store' of undefined

Here’s the index.js for the route:

import Ember from 'ember';

export default Ember.Route.extend({

   beforeModel: function(transition){


       console.log(transition.queryParams.post);

       transition.queryParams.post.forEach(function(item){
   			console.log(item);
        // console.log(this.store)
        //
        // this.store.createRecord('post', {
        //     title: item
        // })
      })

   },

// manually add record using action 
actions:{
    addpost(){

      console.log(this.store)
      this.store.createRecord('post', {
        title: 'a new post'
      });

    }

  }
});

Any ideas on how to go around this? Help much appreaciated! :smile:

Here’s the whole app as a repo: https://github.com/template01/emberparamsstore

All the bests

Lasse

This looks like a scope problem in your forEach loop.

var me = this; transition.queryParams.post.forEach(function(item){ console.log(item); console.log(me.store) // // me.store.createRecord(‘post’, { // title: item // }) });

The error did not show up in your addPost action.

Aha! Thanks! So I should assume that this is not accessible within the loop, but if stored as a global variable, then (tadaa) no problem. :grin:

Yes, except that its not a global variable if stored inside the beforeModel function :slight_smile: In this example me exists only in your beforeModel function. me outside would return undefined

No errors in the console, the laravel action works. That’s the only way to persist data right now. I’ve bwen told that I need to MO e the wollTransistion method out of actions because it’s a hook. The console even shows a 200 request to /libraries