Skip to content

Commit

Permalink
Merge 84c4345 into 0b0f546
Browse files Browse the repository at this point in the history
  • Loading branch information
cherifGsoul committed Oct 2, 2019
2 parents 0b0f546 + 84c4345 commit 47dd464
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 25 deletions.
14 changes: 12 additions & 2 deletions docs/guides/place-my-order.md
Expand Up @@ -799,7 +799,12 @@ First, let's look at the restaurant data we get back from the server. It looks l
}
```

We have a `menu` property which provides a `lunch` and `dinner` option (which will show later inside the tabs we set up in the previous chapter). We want to be able to add and remove items from the order, check if an item is in the order already, set a default order status (`new`), and be able to calculate the order total. For that to happen, we need to create a new `order` model:
We have a `menu` property which provides a `lunch` and `dinner` option (which will show later inside the tabs we set up in the previous chapter). We want to be able to add and remove items from the order, check if an item is in the order already, set a default order status (`new`), and be able to calculate the order total. For that to happen, we need to create a new `item` model:

@sourceref ../../guides/place-my-order/steps/create-data/item.js
@highlight 1-24,only

And generate `order` model:

```shell
donejs add supermodel order
Expand All @@ -808,7 +813,12 @@ donejs add supermodel order
Like the restaurant model, the URL is `/api/orders` and the id property is `_id`. To select menu items, we need to add some additional functionality to `src/models/order.js`:

@sourceref ../../guides/place-my-order/steps/create-data/order.js
@highlight 4-25,34-55,only
@highlight 3,11-32,only

Add `menu` property to the `restaurant` model in `src/models/restaurant.js`:

@sourceref ../../guides/place-my-order/steps/create-data/restaurant.js
@highlight 3,12-21,only

Here we define an `ItemsList` which allows us to toggle menu items and check if they are already in the order. We set up ItemsList as the Value of the items property of an order so we can use its has function and toggle directly in the template. We also set a default value for status and a getter for calculating the order total which adds up all the item prices. We also create another `<order-model>` tag to load orders in the order history template later.

Expand Down
24 changes: 24 additions & 0 deletions guides/place-my-order/steps/create-data/item.js
@@ -0,0 +1,24 @@
import { DefineMap, DefineList} from 'can';

export const Item = DefineMap.extend({
seal: false
}, {
price: 'number'
});

export const ItemsList = DefineList.extend({
'#': Item,
has: function(item) {
return this.indexOf(item) !== -1;
},

toggle: function(item) {
var index = this.indexOf(item);

if (index !== -1) {
this.splice(index, 1);
} else {
this.push(item);
}
}
});
24 changes: 1 addition & 23 deletions guides/place-my-order/steps/create-data/order.js
@@ -1,28 +1,6 @@
import { DefineMap, DefineList, superModel } from 'can';
import loader from '@loader';

const Item = DefineMap.extend('Item', {
seal: false
}, {
price: 'number'
});

const ItemsList = DefineList.extend('ItemList', {
'#': Item,
has: function(item) {
return this.indexOf(item) !== -1;
},

toggle: function(item) {
var index = this.indexOf(item);

if (index !== -1) {
this.splice(index, 1);
} else {
this.push(item);
}
}
});
import { ItemsList } from "./item";

const Order = DefineMap.extend('Order', {
seal: false
Expand Down
35 changes: 35 additions & 0 deletions guides/place-my-order/steps/create-data/restaurant.js
@@ -0,0 +1,35 @@
import { DefineMap, DefineList, superModel } from 'can';
import loader from '@loader';
import { ItemsList } from "./item";

const Restaurant = DefineMap.extend({
seal: false
}, {
'_id': {
type: 'any',
identity: true
},
menu:{
type: {
lunch: {
Type: ItemsList
},
dinner: {
Type: ItemsList
}
}
}
});

Restaurant.List = DefineList.extend({
'#': Restaurant
});

Restaurant.connection = superModel({
url: loader.serviceBaseURL + '/api/restaurants',
Map: Restaurant,
List: Restaurant.List,
name: 'restaurant'
});

export default Restaurant;

0 comments on commit 47dd464

Please sign in to comment.