How to use cosmicjs - 10 common examples

To help you get started, we’ve selected a few cosmicjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cosmicjs / cosmicapp-voting-app / components / AppState.js View on Github external
vote(object) {
    this.is_saving = true
    if (config.cosmicjs.bucket.write_key)
      object.write_key = config.cosmicjs.bucket.write_key
    addObject({ bucket: config.cosmicjs.bucket }, object, (err, res) => {
      // Set local storage
      window.localStorage.setItem(this.poll._id, _.find(this.options_selected, { poll: this.poll._id }).value)
      // Go to results page
      this.showResults()
    })
  }
  showResults() {
github cosmicjs / cosmicapp-voting-app / components / AppState.js View on Github external
showResults() {
    getObjects({ bucket: config.cosmicjs.bucket }, (err, res) => {
      this.is_saving = false
      this.poll.vote_counted = true
      if (res.objects) {
        const votes = res.objects.type.votes
        this.votes = votes
        this.show_results = true
        // Redo totals
        let polls = res.objects.type.polls
        polls = this.getVoteTotals(polls, votes)
        const poll_index = _.findIndex(this.polls, { _id: this.poll._id })
        polls[poll_index].vote_counted = true
        this.polls = polls
      }
    })
  }
  getVoteTotals(polls, votes) {
github tonyspiro / react-chat-app / app-client.js View on Github external
componentDidMount() {
    let data = this.state.data
    setTimeout(() => {
      this.refs.author.refs.input.focus()
    }, 100)
    const socket = io()
    Cosmic.getObjects(config, (err, res) => {
      const messages = res.objects.type.messages
      if (messages) {
        messages.reverse()
        this.setState({
          data: {
            author: data.author,
            messages
          }
        })
      }
    })
    // Listen for messages coming in
    socket.on('chat message', message => {
      data = this.state.data
      const messages = this.state.data.messages
      if (data.author !== message.metafield.author.value) {
github cosmicjs / cosmicapp-voting-app / components / AppState.js View on Github external
constructor() {
    // Get all polls and votes
    getObjects({ bucket: config.cosmicjs.bucket }, (err, res) => {
      if (res.objects) {
        let polls = res.objects.type.polls
        const votes = res.objects.type.votes
        polls = this.getVoteTotals(polls, votes)
        // If already voted
        polls.forEach((poll, i) => {
          if (window.localStorage.getItem(polls[i]._id)) {
            this.options_selected.push({ poll: polls[i]._id, value: window.localStorage.getItem(polls[i]._id) })
            polls[i].vote_counted = true
          }
        })
        this.polls = polls
        this.votes = votes
        const slug = window.location.pathname.replace('/', '')
        if (slug) {
          const poll = _.find(res.objects.type.polls, { slug })
github tonyspiro / react-universal-blog / actions / actions.js View on Github external
export function getStore(callback){
  
  let pages = {}

  Cosmic.getObjects(config, function(err, response){
    
    let objects = response.objects
    
    /* Globals
    ======================== */
    let globals = AppStore.data.globals
    globals.text = response.object['text']
    let metafields = globals.text.metafields
    let menu_title = _.find(metafields, { key: 'menu-title' })
    globals.text.menu_title = menu_title.value

    let footer_text = _.find(metafields, { key: 'footer-text' })
    globals.text.footer_text = footer_text.value

    let site_title = _.find(metafields, { key: 'site-title' })
    globals.text.site_title = site_title.value
github tonyspiro / tonyspiro.com-react / stores / AppStore.js View on Github external
init: function(payload) {
    
    let _this = this;
    let pages = {};

    _this.data.ready = false;
    _this.data.pages = [];

    Cosmic.getObjects(config, function(err, response){
      
      let objects = response.objects;
      
      /* Globals
      ======================== */
      let globals = _this.data.globals;
      globals.text = response.object['text'];
      let metafields = globals.text.metafields;
      let menu_title = _.findWhere(metafields, { key: 'menu-title' });
      globals.text.menu_title = menu_title.value;

      let footer_text = _.findWhere(metafields, { key: 'footer-text' });
      globals.text.footer_text = footer_text.value;

      let site_title = _.findWhere(metafields, { key: 'site-title' });
      globals.text.site_title = site_title.value;
github cosmicjs / medium-backup / routes / import-posts.js View on Github external
content = post.description[0]
            if (post['content:encoded'])
              content = post['content:encoded'][0]
            if (post['pubDate'])
              published_at = post['pubDate'][0]
            if (post['atom:updated'])
              modified_at = post['atom:updated'][0]
            if (post['category'])
              categories = post['category']
            if (post['dc:creator'])
              created_by = post['dc:creator'][0]
            if (post['link'])
              medium_link = post['link'][0]
            // Test if object available
            var slug = slugify(title).toLowerCase();
            Cosmic.getObject(cosmic_config, { slug: slug }, function(err, response) {
              if (response && response.object) {
                // already added
                return callback()
              } else {
                var params = {
                  title: title,
                  slug: slug,
                  content: content,
                  type_slug: 'posts',
                  write_key: config.bucket.write_key,
                  metafields: [
                    {
                      key: 'published_at',
                      title: 'Published At',
                      value: published_at
                    },
github cosmicjs / vuejs-photo-gallery / src / App.vue View on Github external
created() {
    Cosmic.getObjectType({ bucket }, { type_slug: 'globals' }, (err, res) => {
      EventBus.$emit('global_loaded', res.objects.all[0]);
      console.log(res.objects.all[0]);
    });
  },
}
github cosmicjs / facebook-bot / index.js View on Github external
schedule.scheduleJob(time, function(){
    Cosmic.getObject(config, {slug: itemSlug}, function(error, response){
      if(response.object.metadata.date == new Date(time).toISOString()){
        bot.say(BotUserId, response.object.title)
        console.log('firing reminder')
      } else {
        eventEmitter.emit('new', response.object.slug, response.object.metafield.date.value)
        console.log('times do not match checking again at '+response.object.metadata.date)
      }
    })
  })
})
github cosmicjs / notetoself / index.js View on Github external
inquirer.prompt(question).then((answers) => {
    var params = {
      write_key: config.bucket.write_key,
      type_slug: 'notes',
      slug: Random.id().toLowerCase(),
      title: answers.note_text,
      content: ''
    }
    if(answers.note_text === ''){
      console.log(chalk.red("You cannot add an empty note"))
      MainMenu()
      return;
    }
    Cosmic.addObject(config, params, (error, response) =>{
      if(response.object){
        console.log(chalk.green("\u2713" + " Success"))
      }
      if(error){
        console.log(error)
      }
      MainMenu()
    })
  })
}

cosmicjs

The official client module for Cosmic. This module helps you easily add dynamic content to your website or application using the Cosmic headless CMS.

MIT
Latest version published 2 years ago

Package Health Score

42 / 100
Full package analysis