title: Ember Model with only One Instance
body: <p>I'm trying to set up a shopping bag model in Ember. The shopping bag will be created on load of the page and saved in LocalStorage using the LocalStorage adapter. At any given time, there <strong>should only be one instance of a bag</strong> saved, as a user only needs to add products to one shopping bag. My question is this: it seems that I'm being hack-y with my methods of getting and setting data on my bag as Ember data caters to models with more than one instance. Is there a better way to structure/define my bag model that is better suited for one-instance models? Here's my model:</p>
  
  <pre><code>import DS from 'ember-data';
  
  export default DS.Model.extend({
    products: DS.hasMany('product', {async: true}),
    productCount: function() {
      return this.get('products.length');
    }.property('products.length')
  });
  </code></pre>
  
  <p>When I want to get the productCount in my template, the only way I can seem to get it to print is use an <code>{{#each}}</code> statement with <code>{{productCount}}</code> nested inside. As there is only one bag, this seems inefficient. In other parts of my code, I need to get the current instance of the bag and act on it. To get this to work, I'm finding all bags, then getting the <code>firstObject</code>, which also seems hack-y:</p>
  
  <pre><code>import Ember from "ember";
  
  export default Ember.ArrayController.extend({
    actions: {
      addToBag: function(model) {
        this.store.find('bag').then(function(bags) {
          var bag = bags.get('firstObject');
          bag.get('products').then(function(products) {
            products.pushObject(model);
            bag.save();
          });
        });
      }
    }
  });
  </code></pre>
  
  <p>My application route uses the bag as its model, and sets up the controller:</p>
  
  <pre><code>import Ember from "ember";
  
  var ApplicationRoute = Ember.Route.extend({
    activate: function() {
      var store = this.store;
      store.find('bag').then(function(bags) {
        var existing_bag = bags.get('firstObject');
        // If there isn't already a bag instantiated, make one and save it
        if(typeof existing_bag === 'undefined') {
          var new_bag = store.createRecord('bag');
          new_bag.save();
        }
      });
    },
    model: function() {
      return this.store.find('bag');
    },
    setupController: function(controller,model) {
      controller.set('content', model);
    }
  });
  </code></pre>
  
  <p>Any ideas here to make this more efficient? I don't want this to fester into code that is messy. Thanks so much in advance!</p>