KNOCKOUT:Uncaught (inpromise) referenced error

114 Views Asked by At

I am learning knockout and trying out a small example below are the the three files that I have: index introduction introduction

I am using netbeans IDE for the development .

index.html

<!DOCTYPE html>

<html>
<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script data-main="js/main" src="js/libs/require/require.js"     type="text/javascript"></script>
    <link href="css/libs/oj/v2.1.0/alta/oj-alta-min.css" rel="stylesheet" type="text/css"/>
    <style>
        table, th, td {
border: 1px solid black;
padding: 15px;
}
th {
text-align: left;    
}
 thead{
 border-style: double;
 font-weight:  bold ;
 }
   tr {
text-align: left;
  }
  {background-color: #f5f5f5}
    </style>
</head>
<body>
    <div data-bind="ojModule: {name: 'introduction'}"></div>
</body>
  </html>

viewModels - introduction.js

/**
 * introduction module
*/
define(['ojs/ojcore', 'knockout',oj,jquery,require
], function (oj, ko) {
/**
 * The view model for the main content view template
 */
function introductionContentViewModel() {
    var self = this;
    self.firstName = ko.observable("Planet");
  self.lastName = ko.observable("Earth");

    self.fullName = ko.pureComputed(function () {
        return this.firstName() + " " + this.lastName();
    }, this);
    this.fullName= this.firstName() +" " +this.lastName();

this.resetName=function(){
alert("Reset Name!");
this.firstName("James");
this.lastName("Potter");
};  

this.capitalizeName=function(){
var curValue=this.lastName();
this.lastName(curValue.toUpperCase());
};

     function seatReservation(fname,lname, reservMeals){
        this.firstName=fname;
        this.lastName=lname;
        this.meals = ko.observable(reservMeals);
     /*   this.formattedPrice=ko.computed(function(){
        var price = this.meals.price;
        return price ? "$" + price.toFixed(2):"none";
    });*/
    };       

      this.mealsAvailable=[{mealName:"SandWich",price:25},
        {mealName:"Roti",price:23},
        {mealName:"Dal",price:22}];

    self.seats = ko.observableArray([
    new seatReservation("Steve","Hawkins", this.mealsAvailable[0]),
    new seatReservation("Bert","Baltymoore", this.mealsAvailable[1])
  ]);

//function to add new reservation into the table
this.newReservationRow=function(){

    this.seats.push(new seatReservation("","",this.mealsAvailable[0]));
};    
}

return introductionContentViewModel;
});

views -introduction.html

 <body>
<form>
 <div class='liveExample'>   
<p> First Name: <span data-bind='text: firstName'/> </p>
<p> Last Name: <span data-bind='text: lastName'/> </p>
<p>First name: <input data-bind='value: firstName' /></p> 
<p>Last name: <input data-bind='value: lastName' /></p> 
<h2>Hello, <span data-bind='text: fullName'> </span>!</h2>  
<button data-bind='click: resetName' >Reset Name </button>
<button data-bind='click: capitalizeName'>Capitalize </button>
<input type='submit' data-bind='click: resetName' value='Reset'/>
</div>
<div class="Reservations">
<h2>Reservations </h2>
<table>
<thead> <tr><td> FirstName </td><td> LastName</td> <td> Meals</td><td>         Price</td></tr></thead>
<tbody data-bind="foreach: seats">
<tr>
    <td><input data-bind="value: firstName"/> </td>
    <td><input data-bind="value: lastName"/> </td>
    <td><select data-bind="options: meals,optionsText:'mealName'"></select>      </td>
    <td data-bind="text: meals().price"> </td>
   </tr>
   </tbody>
   </table><br>
   <input type="submit" value="New Reservation" label="New Reservation"    title="Click to Make a New Reservation" data-bind="click: newReservationRow"/>
    </div>
   </form>
   </body>

I am not getting the desired out put. The desired output is something like this in the below link

http://learn.knockoutjs.com/#/?tutorial=collections

1

There are 1 best solutions below

0
peppertech On

You are mixing self and this a lot in your code. I recommend cleaning that up first and see if things start working for you. Personally, I like to stay with self.xxxx format.

Also, remove the reference to "require" inside of your define block in the introduction.js file. That may be causing some issues. Either way, it's not needed.

Finally, it appears you are doing all of this using Oracle JET. Since the introduction.html is a view that will be used inside of ojModule, you do not need to have the < body> element defined a second time. The introduction.html is going to be a fragment that will take the place of the < div> that you have bound to ojModule.