|
|
The complete code for this section can be found in Appendix 3 in GitHub.
|
|
Function
|
Description
|
Code
|
||
|---|---|---|---|---|
|
addToCart
|
Adds the selected parts to the cart. This function is created as a standalone function.
|
Complete the following steps:
1. Below the hilite function, initialize the cartLabel application parameter to just say Cart.
This will be the beginning text in the labelCart label and will change as items are added to the cart. Also, initialize an empty object named cart. Add the following below the hilite function:
$scope.app.params.cartLabel = "Cart"; // set cartLabel app parameter to be "Cart". This will bind to the Text property for the labelCart label 2. Start the addToCart function. A variable called cartItem is created and initialized to be the value of the property in cart that is equal to the part that is currently selected.
The first time that a part is selected, this property will be undefined because the cart was initialized as an empty object.
If the current selection has not yet been added to the cart object, otherwise called undefined, then cartItem will become populated with the itemCount, itemNumber, itemName, and priceInfo application parameters for the selected part.
All of these application parameters will be set as property values inside the cartItem object. If the item has already been added to the cart once, then the only property in cartItem that will change is the itemCount parameter, which will increase by 1, since another instance of that same part has been added to the cart.
After this if statement, the cartItem object and its properties will be added to the cart object. This process helps differentiate between the different parts that are added to the cart.
Add the following after $scope.cart = {}; // declare empty object called cart:
// 3. Initialize more variables for the function. cartItemAmount will be used to count the number of items in the cart, cartContents will be initialized as an empty array that will hold the contents of the cart, and cartPrice will be the total price of the objects in the cart. Add the following after $scope.cart[$scope.currentSelection] = cartItem;:
// 4. A for loop will be used to loop over each item that is in the cart to check for the following conditions.
itm will be the counting variable for the loop. In this loop, the count property for the object corresponding to the selected part will be increased for each time that another instance of the part is added to the cart.
In addition to this, cartPrice, the price variable, will increase based on the prc property in the cart for the selected item.
When an item is added to the cart, the price will increase by the price of the part multiplied by the quantity of parts added.
Using the .push method, the name (tag), quantity (count), and price (prc) of the selected object will be pushed into the cartContents array. The cartContents array will then be set to be equal to the cart application parameter.
Add the following code after var cartPrice = 0;:
// 5. The last portion of the addToCart function involves editing the text for the labelCart label by adding a value to the cartLabel application parameter.
Add the following code after $scope.app.params.cart = cartContents;:
// |
||
|
clearCart
|
This function will clear the cart when the clearButton button is clicked. This function sets the application parameter for cart to be an empty array, sets the cart object to be empty, and changes the Text property for the labelCart button to be set back to Cart without a price total using the cartLabel application parameter.
|
Add the following code after the addToCart function:
// |
||
|
hiliteOff
|
This function makes sure that the shader is removed from the model when the popup closes. The hiliteOff function will be created inside the PTC API below where the shader is added to the part.
|
Add the following code directly below $scope.hilite([$scope.currentSelection], true);
// |
||
|
disassemble
|
The disassemble function will be created inside the PTC API, as well, and is used to add the functionality to the Disassemble button on the popup. It uses the same logic used with the playButton and triggers the sequenceloaded event listener when the sequence of the model is set.
|
Add the following code directly below the hiliteOff function that you just created:
// |