How do I pass an object or set a variable in the TVML template file that I use?

I am working with sample TVMLCatalog files from Apple, and I am stuck trying to transfer the object to a template file that I load in the presenter (javascript file). It seems like it should be a completely rudimentary thing to do, but it hit me.

I have the following that loads a template with a resource loader and pops it into the view.

resourceLoader.loadResource('http://localhost/mytemplate.xml.js',
                function(resource) {
                    if (resource) {
                        var doc = self.makeDocument(resource);
                        doc.addEventListener("select", self.load.bind(self));
                        navigationDocument.pushDocument(doc);
                    }
                }
            );

Where can I define an object or set a variable that will be in the document when the template file is loaded into the view?

+4
source share
1 answer

Yes! You can embed variables in your TVML templates.

, TVML, ${variable} . DOMParser XML DOM. , currentModal (main object navigationDocument)

:

function catalogTemplate(title, firstMovie, secMovie) {
  var xmlStr = `<?xml version="1.0" encoding="UTF-8" ?>

   <document>
   <catalogTemplate>
  <banner>
   <title>${title}</title>
 </banner>
  <list>
 <section>
  <listItemLockup>
  <title>All Movies</title>
 <decorationLabel>2</decorationLabel>
 <relatedContent>
  <grid>
  <section>
 <lockup>
  <img src="http://a.dilcdn.com/bl/wp-content/uploads/sites/2/2014/03/Maleficent-Poster.jpg" width="250" height="376" />
  <title>${firstMovie}</title>
  </lockup>
  <lockup>
  <img src="http://www.freedesign4.me/wp-content/gallery/posters/free-movie-film-poster-the_dark_knight_movie_poster.jpg" width="250" height="376" />
  <title>${secMovie}</title>
  </lockup>
  </section>
  </grid>
  </relatedContent>
  </listItemLockup>

  </section>
 </list>
  </catalogTemplate>
 </document>`

  var parser = new DOMParser();
  var catalogDOMElem = parser.parseFromString(xmlStr, "application/xml");
  navigationDocument.presentModal(catalogDOMElem );
}

PS: . template

onLaunch catalogTemplate, .

App.onLaunch = function(options) {    
catalogTemplate("title", "Maleficent.", "The Dark knight");
}

addEventListener

function catalogTemplate(title, firstMovie, secMovie, cb) {
  var xmlStr = `<?xml version="1.0" encoding="UTF-8" ?>
 <document>
   <catalogTemplate>
  <banner>
   <title>${title}</title>
 </banner>
  <list>
 <section>
  <listItemLockup>
  <title>All Movies</title>
 <decorationLabel>2</decorationLabel>
 <relatedContent>
  <grid>
  <section>
 <lockup>
  <img src="http://a.dilcdn.com/bl/wp-content/uploads/sites/2/2014/03/Maleficent-Poster.jpg" width="250" height="376" />
  <title>${firstMovie}</title>
  </lockup>
  <lockup>
  <img src="http://www.freedesign4.me/wp-content/gallery/posters/free-movie-film-poster-the_dark_knight_movie_poster.jpg" width="250" height="376" />
  <title>${secMovie}</title>
  </lockup>
  </section>
  </grid>
  </relatedContent>
  </listItemLockup>

  </section>
 </list>
  </catalogTemplate>
 </document>

`
  var parser = new DOMParser();
  var catalogDOMElem = parser.parseFromString(xmlStr, "application/xml");

 catalogDOMElem.addEventListener("select", cb, false);

  navigationDocument.presentModal(catalogDOMElem );
}

, , , .

function ratingTemplate(title) {
  var xmlStr = `<?xml version="1.0" encoding="UTF-8" ?>
  <document>
    <ratingTemplate>  
<title>${title}</title>
<ratingBadge value="0.8"></ratingBadge>
</ratingTemplate>
  </document>`
  var parser = new DOMParser();
  var ratingDOMElem = parser.parseFromString(xmlStr,"application/xml");
  navigationDocument.presentModal(ratingDOMElement);
}

onLaunch.

App.onLaunch = function(options) {    

catalogTemplate("title", "Maleficent.", "The Dark knight", function() {
        navigationDocument.dismissModal();

        ratingTemplate("rating template title")

    });
}

Check out this list for more tutorials .

+3
source

Source: https://habr.com/ru/post/1608179/


All Articles