JavaScript for WordPress › Forums › Vanilla JavaScript › Function to get both pages and posts
- This topic has 0 replies, 1 voice, and was last updated 7 years, 3 months ago by
David.
Viewing 1 post (of 1 total)
-
AuthorPosts
-
October 17, 2017 at 11:36 am #32426
David
ParticipantWorking on the VP0.2 – updating the model, completed. I wanted to create a function
that could be used to select both pages and posts as suggested in the video.
in the model.js there is a model.getPosts function. I added a modified version to
select both posts and pages but when I run that function I get this errorUncaught TypeError: model.getLocalStore is not a function at Object.model.getContent (model.js:44) at model.js:50 model.js:17 Uncaught TypeError: model.updateLocalStore is not a function at Object.model.init (model.js:17) at Object.vanillaPress.init (app.js:20) at app.js:26
Here is the code
model.getPosts = function() { var posts = model.getLocalStore().posts; return posts; } //***************************************************** //my code to condense getting pages and posts in the same function var whichType =''; model.getContent = function(whichType) { if (whichType === 'posts'){ console.log(model.getLocalStore().posts); } } //****************************************** console.log(model.getContent('posts'));
Here is the complete page
/** * Model file for working with data */ /** * Main Model Object * */ var model = {}; /** * Initializes the Model * */ model.init = function() { model.updateLocalStore( data ); } /** * Gets posts from local store * * @return {Object[]} posts Array of posts */ model.getPosts = function() { var posts = model.getLocalStore().posts; return posts; } //***************************************************** //my code to condense getting pages and posts in the same function var whichType =''; model.getContent = function(whichType) { if (whichType === 'posts'){ console.log(model.getLocalStore().posts); } } //****************************************** console.log(model.getContent('posts')); /** * Get a single post based on url slug * * @param {string} slug The slug for the post * @return {Object} post Single post * */ model.getPost = function( slug ) { var posts = model.getLocalStore().posts; // Get the post from store based on the slug for( i = 0, max = posts.length; i < max; i++ ) { if( slug === posts[i].slug ) { return posts[i]; } } return null; } /** * Gets pages from local store * * @return {Object[]} pages Array of page objects */ model.getPages = function() { var pages = model.getLocalStore().pages; return pages; } /** * Get a single page based on url slug * * @param {String} slug The slug for the page * @return {Object} page Single page object * */ model.getPage = function( slug ) { var pages = model.getLocalStore().pages; // Get the post from store based on the slug for( i = 0, max = pages.length; i < max; i++ ) { if( slug === pages[i].slug ) { return pages[i]; } } return null; } /** * Gets content from local store * * @return {Object} store Native JavaScript object from local store */ model.getLocalStore = function() { var store = JSON.parse( localStorage.getItem( 'vanillaPress' ) ); return store; } /** * Saves temporary store to local storage. * * @param {Object} store Native JavaScript object with site data */ model.updateLocalStore = function( store ) { localStorage.setItem( 'vanillaPress', JSON.stringify( store ) ); } /** * Deletes data from local storage * */ model.removeLocalStore = function() { localStorage.removeItem( 'vanillaPress' ); }
-
AuthorPosts
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.