How to use the rails-ujs.ajax function in rails-ujs

To help you get started, we’ve selected a few rails-ujs 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 getcampo / campo / app / webpacker / javascripts / controllers / form_validation_controller.js View on Github external
validateRemote(event) {
    let input = event.target
    let formData = new FormData()
    formData.append('value', input.value)
    Rails.ajax({
      url: input.dataset.validateUrl,
      type: 'post',
      data: formData,
      success: (data) => {
        console.log(data)
        let field = input.closest('.form-field')
        let message = field.querySelector('.validate-message')

        if (data.valid) {
          field.classList.remove('invalid')
          field.classList.add('valid')
          if (message) {
            message.remove()
          }
        } else {
          field.classList.add('invalid')
github getcampo / campo / app / webpacker / javascripts / controllers / editor_controller.js View on Github external
alert(`${file.name} is too big! Should be less than 10MB.`)
      } else {
        let fileName = file.name
        let pendingTag
        if (file.type.startsWith('image/')) {
          pendingTag = `![Uplaoding ${fileName}...]()\n`
        } else {
          pendingTag = `[Uplaoding ${fileName}...]()\n`
        }
        let start = this.inputTarget.selectionStart
        let end = this.inputTarget.selectionEnd
        document.execCommand('insertText', false, pendingTag)
        this.inputTarget.setSelectionRange(start + pendingTag.length, start + pendingTag.length)
        let formData = new FormData()
        formData.append('attachment[file]', file)
        Rails.ajax({
          url: '/attachments',
          type: 'POST',
          dataType: 'json',
          data: formData,
          success: (data) => {
            let start = this.inputTarget.selectionStart
            let end = this.inputTarget.selectionEnd
            let content;
            if (file.type.startsWith('image/')) {
              content = `![${data.filename}](${data.url})\n`
            } else {
              content = `[${data.filename}](${data.url})\n`
            }
            this.inputTarget.value = this.inputTarget.value.replace(pendingTag, content)
            this.inputTarget.setSelectionRange(start + content.length, end + content.length)
          }
github getcampo / campo / app / webpacker / javascripts / controllers / topic_controller.js View on Github external
newPost() {
    let url = new URL(location.href)
    url.searchParams.set('position', this.postsTarget.dataset.total)
    Rails.ajax({
      url: url.href,
      type: 'get',
      dataType: 'html',
      success: (data) => {
        this.postsTarget.outerHTML = data.querySelector('#posts').outerHTML
        this.recalculateIndex()
        this.updateSlider()
        this.parseReaction(this.postsTarget.dataset.likePostIds, 'like')
        this.parseReaction(this.postsTarget.dataset.dislikePostIds, 'dislike')
        document.querySelector('#post-new-form textarea').focus()
        this.floatingActionTarget.floatingActionController.close()
      }
    })
  }
github getcampo / campo / app / webpacker / javascripts / controllers / topic_controller.js View on Github external
loadBefore() {
    if (this.postsTarget.dataset.reachedBegin) {
      return
    }

    this.loading = true
    this.loadingBeforeTarget.classList.remove('display-none')
    let url = new URL(location.href)
    url.searchParams.set('before', this.postsTarget.dataset.beginId)
    Rails.ajax({
      url: url.href,
      type: 'get',
      dataType: 'html',
      success: (data) => {
        let oldHeight = this.postsTarget.offsetHeight
        let oldScrollY = window.scrollY
        let postsElement = data.querySelector('#posts')
        this.postsTarget.insertAdjacentHTML('afterbegin', postsElement.innerHTML)
        this.postsTarget.dataset.beginId = postsElement.dataset.beginId
        this.postsTarget.dataset.reachedBegin = postsElement.dataset.reachedBegin
        this.postsTarget.dataset.offset = postsElement.dataset.offset
        window.scrollTo(0, oldScrollY + (this.postsTarget.offsetHeight - oldHeight))
        this.recalculateIndex()
        this.updateSlider()
        this.parseReaction(postsElement.dataset.likePostIds, 'like')
        this.parseReaction(postsElement.dataset.dislikePostIds, 'dislike')
github getcampo / campo / app / webpacker / javascripts / controllers / editor_controller.js View on Github external
preview() {
    this.previewOutputTarget.textContent = 'Loading...'
    this.element.classList.add('previewing')
    let formData = new FormData()
    formData.append('content', this.inputTarget.value)
    Rails.ajax({
      url: '/preview',
      type: 'POST',
      data: formData,
      success: (data, statusText, xhr) => {
        this.previewOutputTarget.innerHTML = xhr.responseText
      }
    });
  }
}
github getcampo / campo / app / webpacker / javascripts / controllers / topic_controller.js View on Github external
visitPosition(event) {
    let url = new URL(location.href)
    url.searchParams.set('position', event.detail.begin)
    Rails.ajax({
      url: url.href,
      type: 'get',
      dataType: 'html',
      success: (data) => {
        this.postsTarget.outerHTML = data.querySelector('#posts').outerHTML
        this.recalculateIndex()
        this.updateSlider()
        this.focuspost()
        this.parseReaction(this.postsTarget.dataset.likePostIds, 'like')
        this.parseReaction(this.postsTarget.dataset.dislikePostIds, 'dislike')
      }
    })
  }
github getcampo / campo / app / webpacker / javascripts / controllers / infinite_page_controller.js View on Github external
}

    if (this.data.get('reachedEnd') == 'true') {
      this.disconnect()
      return
    }

    let rect = this.element.getBoundingClientRect()
    let distance = rect.height - (window.innerHeight - rect.top)
    if (distance < this.threshold) {
      this.loading = true
      this.element.classList.add('loading')
      let nextPage = parseInt(this.data.get('page')) + 1
      let url = new URL(location.href)
      url.searchParams.set('page', nextPage)
      Rails.ajax({
        url: url.href,
        type: 'get',
        dataType: 'html',
        success: (data) => {
          const page = data.querySelector('[data-controller~=infinite-page]')
          this.data.set('page', page.dataset.infinitePagePage)
          this.data.set('reachedEnd', page.dataset.infinitePageReachedEnd)

          const container = data.querySelector('[data-target~="infinite-page.container"]')
          this.containerTarget.insertAdjacentHTML('beforeend', container.innerHTML)
        },
        complete: () => {
          this.loading = false
          this.element.classList.remove('loading')
        }
      })
github getcampo / campo / app / webpacker / javascripts / controllers / topic_controller.js View on Github external
loadAfter() {
    if (this.postsTarget.dataset.reachedEnd) {
      return
    }

    this.loading = true
    this.loadingAfterTarget.classList.remove('display-none')
    let url = new URL(location.href)
    url.searchParams.set('after', this.postsTarget.dataset.endId)
    Rails.ajax({
      url: url.href,
      type: 'get',
      dataType: 'html',
      success: (data) => {
        let postsElement = data.querySelector('#posts')
        this.postsTarget.insertAdjacentHTML('beforeend', postsElement.innerHTML)
        this.postsTarget.dataset.endId = postsElement.dataset.endId
        this.postsTarget.dataset.reachedEnd = postsElement.dataset.reachedEnd
        this.recalculateIndex()
        this.updateSlider()
        this.parseReaction(postsElement.dataset.likePostIds, 'like')
        this.parseReaction(postsElement.dataset.dislikePostIds, 'dislike')
      },
      complete: () => {
        this.loading = false
        this.loadingAfterTarget.classList.add('display-none')

rails-ujs

Ruby on Rails unobtrusive scripting adapter

MIT
Latest version published 2 years ago

Package Health Score

78 / 100
Full package analysis

Similar packages