View.js: An ex­pla­na­tion

OpenSource For You - - HOW TO -

Line 1 uses the Jquery ready state­ment, to start the in­ter­pre­ta­tion af­ter load­ing HTML. Line 3 sim­ply de­fines the tem­plate as a string; it is com­piled by the state­ment _.tem­plate() and re­turns the func­tion. This is called pre­com­pi­la­tion. This is ex­plained in de­tail in the next sec­tion. This tem­plate uses the interpolate tag <%= course %>. It prints the con­tents of the course vari­able. In Line 4, data is passed to the func­tion tpl. This func­tion re­places the vari­able with data and it re­turns the HTML con­tent. Line 5 in­serts the HTML con­tent to the <header> us­ing the Jquery func­tion. Lines 7-22 de­fine the sam­ple JSON data. In real-time, it might be fetched from the server side API by us­ing an AJAX re­quest. Lines 24-26 also work in the same way as Lines 3–5, but it takes the tem­plate from the sep­a­rate script tag by us­ing the Jquery se­lec­tor.

tag by

In the above ex­am­ple, Interpolate looks for the tags like {{ … }}. For the eval­u­ate and es­cape tags, we keep the de­fault Un­der­score tem­plate syn­tax. If you want to change syn­tax for a spe­cific tag, keep the de­fault val­ues for the other tags like in the above ex­am­ple. Oth­er­wise, it won’t recog­nise those el­e­ments.

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.