Mihail Ivanchev
Mihail Ivanchev

Reputation: 425

Uncaught ReferenceError with a seemingly defined variable

So I have this template that utilizes KendoUI to render a grid. Here's a part of it:

<script id="rowTemplateCourse" type="text/x-kendo-tmpl">
  <tr data-cid="#: id #" class="course-row" id="course-row#: id #">
    <td>
      <span class="circle-indicator label-#if(package_is_active == 1){#success#}else{#danger#}#"></span>
    </td>
    <td>
      #: course_name # - #= name#
    </td>
    <td>
      <span class="badge element-bg-color-blue">ver. #:version_number#</span>
    </td>
  </tr>
</script>

I get the needed information from a php controller, that loads a variable in my view that holds this template. Variable holds this sort of data:

[1] => Array(
    [id] => 544
    [course_name] => Course for whatever
    [price] => 52
    [logo] => assets/images/new_course.png
    [version_number] => 1
    [parent_version_id] => 0
    [course_price] => 52.00
    [description_for_school] => 
    [is_print_only] => 0
    [offer_pdf] => 0
    [pdf_final_price] => 0.00
    [simple_course] => 0
    [state_id] => 50
    [name] => Tennessee
    [cs_days_to_complete] => 120
    [course_is_active] => 1
    [user_in_course] => no
    [user_is_waiting] => no
    [days_to_complete] => 0)

In my view, I parse this variable like so:

var course_data = JSON.parse('<?php print(json_encode($courses));?>');

This works correctly and returns the same data like so (copy from console.log):

1: Object
course_is_active:"1"
course_name:"Course for whatever"
course_price:"52.00"
cs_days_to_complete:"120"
days_to_complete:0
description_for_school:""
id:"544"
is_print_only:"0"
logo:"assets/images/new_course.png"
name:"Tennessee"
offer_pdf:"0"
parent_version_id:"0"
pdf_final_price:"0.00"
price:"52"
simple_course:"0"
state_id:"50"
user_in_course:"no"
user_is_waiting:"no"
version_number: "1"

I load the data in a grid like so:

var courses_grid = $("#courses_grid").kendoGrid({
  dataSource: {
    data: course_data,
    schema: {
      model: {
        fields: {
          id: {
            type: "number"
          },
          course_name: {
            type: "string"
          },
          course_short_description: {
            type: "string"
          }
        }
      }
    },
    pageSize: 10,
  },
  toolbar: kendo.template($("#course-header-template").html()),
  rowTemplate: kendo.template($("#rowTemplateCourse").html()),
  groupable: false,
  sortable: true,
  selectable: "single",
  pageable: {
    refresh: true,
    pageSizes: true,
    buttonCount: 5
  },
  columns: [{
    title: "Status",
    width: 100
  }, {
    title: "Course Name",
  }]
});

When the page loads, I get an error that course_is_active is not defined. I don't see how it's not defined, as it is clearly here and has a value. Can someone help me figure this out?

More info on the error:

    Uncaught ReferenceError: course_is_active is not defined
(function(data
/**/) {
var $kendoOutput, $kendoHtmlEncode = kendo.htmlEncode;with(data){$kendoOutput='\n\t <tr data-cid="'+$kendoHtmlEncode( id )+'" class="course-row" id="course-row'+$kendoHtmlEncode( id )+'">\n          <td>\n              <span class="circle-indicator label-';if(course_is_active == 1){;$kendoOutput+='success';}else{;$kendoOutput+='danger';};$kendoOutput+='"></span>\n          </td>\n\t\t  <td>\n               '+$kendoHtmlEncode( course_name )+' - '+( name)+'\n\t\t   </td>\n\t\t\t<td>\n               <span class="badge element-bg-color-blue">ver. '+$kendoHtmlEncode(version_number)+'</span>\n\t\t   </td>\n\t </tr>\n\n';}return $kendoOutput;
})

Upvotes: 2

Views: 298

Answers (1)

Mihail Ivanchev
Mihail Ivanchev

Reputation: 425

I have found the problem. In my PHP code, I am checking in the arrays if a value is equal to 0 and if it is, I am removing that element from the array. This happens to be the first element in the 2D array I load in the view, so when KendoUI starts to load variables in the table, it starts with the [0] index, that does not exist, and throws an error. Thanks to everyone that participated.

Upvotes: 1

Related Questions