Question

I'm trying to create a transaction to create a new user.

From the ember-data.js src:

To create a new transaction, call the transaction() method of your
application's DS.Store instance:

  var transaction = App.store.transaction();

I'm trying to create the transaction in a controller

App.CreateUserController = Ember.Controller.extend({
    addUser: function() {
        var transaction = App.store.transaction();
    }
});

but I'm getting

TypeError: App.store is undefined

I had this store defined

App.Store = DS.Store.extend({
  revision: 11,
  adapter: App.Adapter.create({})
});

it used a captial S so I tried changing App.store to App.Store and I get

TypeError: App.Store.transaction is not a function

If I change my App.Store to use a lowercase s I get

TypeError: store is undefined

Am I missing something?

Was it helpful?

Solution 2

You need to create a store instead of extending it. The convention is to use lowercase letters when you create an instance, so instead of App.Store, you should create a store and name it App.store

App.store = DS.Store.create({
  revision: 11,
  adapter: App.Adapter.create({})
});

Then you'll be able to create a transaction

var transaction = App.store.transaction();

OTHER TIPS

Transaction in Ember.js is gone: save the individual record. https://github.com/emberjs/data/blob/master/TRANSITION.md

You've defined App.Store, which is a class; you are extending DS.Store, which is an existing class. The code to create a transaction should be called on an instance of the store (by convention, classes are capitalized and instances lowercase ).

To create an instance of your defined store:

App.store = App.Store.create();
myTransaction = App.store.transaction();

Note that you extend the Store class to get your own Store class, and you call create on your own store class to instantiate it.

See What's the difference between ember.js extend and create? for more information on extend vs create.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top