GithubHelp home page GithubHelp logo

shaynekasai / v-fetch Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 1.0 423 KB

Vue 2 directive to easily add AJAX requests to your application

License: MIT License

JavaScript 100.00%
vue ajax async http request vuejs2 javascript vue-directive vue-plugin

v-fetch's Introduction

LinkedIn Stargazers License NPM


v-fetch

v-fetch is a Vue directive to add AJAX to your app without the boilerplate


Summary

v-fetch is a directive that adds AJAX functionality to your Vue 2 application so that you don't have to write all of the boilerplate code to call a simple API request to update a model or submit data to an end-point. The goal of v-fetch is to reduce the amount of code in your components and provide a more intuitive interface to making HTTP requests.

Note: This is a work in progress, so the code and API is likely to change quickly.

Installation

npm install @shaynekasai/v-fetch --save

Usage

First, import and use v-fetch:

import VueFetch from 'v-fetch'

Vue.use(VueFetch);

Simple GET example:

<a href="/api/endpoint" v-fetch v-on:click.prevent>click</a>

Simple GET example that updates a model from the AJAX return call:

<span>{{ message }}</span>
<a href="/api/endpoint" v-fetch="{updateModel: 'message'}" v-on:click.prevent>click</a>

Important! when using updateModel, make sure your end-point returns data using the same name. If you want to access a nested value in the json that is returned from your end-point, see the returnField option below

Simple form example that sends foo as json to /api/endpoint:

<form method="post" action="/api/endpoint" v-fetch>
  <input type="hidden" name="foo" value="bar">
</form>

Form POST example that sends formModel data as json and updates the message data from the return request:

<span>{{ message }}</span>
<a href="/api/endpoint" v-fetch:post="{sendModel: 'formModel', updateModel: 'message'}" v-on:click.prevent>click</a>

API

Methods:

v-fetch:get|post|put|patch|delete

Options:

  • updateModel: '<string>' - the Vue model property to update
  • sendModel: '<string>' - the Vue model to send over as a form, json, or query args
  • sendAs: 'json|form' - send data as json data or as FormData
  • returnField: '<string>' - gets the value from your json end-point using dot notation (arrays/more complex notation not supported yet)
  • eventType: '<string>' - the event type to use
  • extraParams: <object> - these get merged into fetch's extra options.
  • onStart: '<string>' - calls your method just before the ajax call
  • onComplete: '<string>' - calls your method after ajax call is completed
  • onError: '<strong> - calls your method if there's an error

Examples

Here are some codepen examples where you can see how this all works:

Callbacks

onStart - before async call is made, includes params used in fetch

onComplete - after async call is made, includes params used in fetch

onError - on async error, call is made, includes params used in fetch

Events

v-fetch:start

v-fetch:complete

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.