Search code examples
angularjsjsdatajs-data-angular

Using jsdata objects without persisting them


I'm using js-data and js-data-angular. I have 2 resources, organization and address, an organization hasOne address.

I'm trying to create an organization (without persisting it) to be able to bind it to the html components. And set some default props. So I'm doing:

var address = AddressResource.createInstance();
address.set('country', defaultCountry);    // CountryResource.get('US');
var organization = OrganizationResource.createInstance({ address: address });

Then I'm doing in the view something like

<input ng-model="organization.address.country.name">

But the address is not being set to the org, since it doesn't exists yet in the store. So is there any way to handle this?

Thanks!


Solution

  • You can disable the relation property accessors, which are looking in the store for the related data. Disabling property accessors means the links won't be kept up-to-date as data changes or is removed from or added to the store.

    var Organization = store.defineResource({
      name: 'organization',
      relations: {
        hasOne: {
          address: {
            // disable property accessor for this relation
            link: false,
            localField: 'address',
            localKey: 'address_id'
          }
        }
      }
    })
    

    Setting linkRelations: false on the store will disable property accessors for all relations.

    js-data 3.x will introduce more flexible options.