<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.rawgit.com/js-data/js-data/master/dist/js-data.js"></script>
<script src="script.js"></script>
</head>
<body>
<h1>Open up your console to see the output</h1>
</body>
</html>
var store = new JSData.DS();
var User = store.defineResource({
name: 'user',
methods: {
say: function() {
return 'hi';
}
}
});
var user = User.createInstance();
console.log('User.createInstance()', user);
var user2 = store.createInstance('user');
console.log('store.createInstance(\'user\')', user2);
console.log('user instanceof User[User.class]', user instanceof User[User.class]);
console.log('user2 instanceof User[User.class]', user2 instanceof User[User.class]);
console.log('user.say()', user.say());
console.log('user2.say()', user2.say());
var user3 = User.createInstance({
name: 'John'
}, {
useClass: false
});
console.log('User.createInstance({ name: \'John\' }, { useClass: false })', user3);
var user4 = store.createInstance('user', {
name: 'John'
}, {
useClass: false
});
console.log('store.createInstance(\'user\', { name: \'John\' }, { useClass: false })', user4);
console.log('user3', user3);
console.log('user3 instanceof User[User.class]', user3 instanceof User[User.class]);
console.log('user4', user4);
console.log('user4 instanceof User[User.class]', user4 instanceof User[User.class]);
try {
console.log('user3.say()');
user3.say();
} catch (e) {
console.log(e.message);
}
try {
console.log('user4.say()');
user4.say();
} catch (e) {
console.log(e.message);
}
## DS#createInstance(resourceName, attrs[, options])
Return a new instance of the specified resource.
###### Arguments
| name | type | description |
| ---- | ---- | ----------- |
| resourceName | string | The name of the resource to use. Unnecessary if using the resource directly. |
| attrs | object | The attributes of the new instance. |
| options | object | Configuration options. Also passed through to the adapter and (conditionally) to `DS.inject`. |
| options.useClass | boolean | Whether to wrap the injected item with the resource's instance constructor. Default: `true`. |
###### Examples
```js
var User = store.defineResource({
name: 'user',
methods: {
say: function () {
return 'hi';
}
}
});
var user = User.createInstance();
var user2 = store.createInstance('user');
user instanceof User[User.class]; // true
user2 instanceof User[User.class]; // true
user.say(); // hi
user2.say(); // hi
var user3 = User.createInstance({ name: 'John' }, { useClass: false });
var user4 = store.createInstance('user', { name: 'John' }, { useClass: false });
user3; // { name: 'John' }
user3 instanceof User[User.class]; // false
user4; // { name: 'John' }
user4 instanceof User[User.class]; // false
user3.say(); // TypeError: undefined is not a function
user4.say(); // TypeError: undefined is not a function
```