Code Monkey home page Code Monkey logo

jquery.execute-object's Introduction

jQuery Execute Object plugin

This is a tiny jQuery plugin that enables you to interpret an object as DOM manipulation instructions with a single function call.

Usage

$.executeObject({"#my_div": ["text", "new text"]})   // object passed
$.executeObject('{"#my_div": ["text", "new text"]}') // JSON is ok too

Use cases

Instead of

$('table tr:first').remove();
$('input#name').val('John');
$('#my_div').addClass('error').text('Error occured');

create an object and pass it to the executeObject function:

var obj =
  { 'table tr:first': 'remove'
  , 'input#name': ['val', 'John']
  , '#my_div':
    [ ['addClass', 'error']
    , ['text', 'Error occured']
    ]
  }

$.executeObject(obj)

Specifically, if you find yourself creating horrendous view templates in vein of

$('input#name').val('<%= escape_javascript(@person.name) %>')
$('#my_div').addClass('error').text('<%= escape_javascript(@error) %>')

you may simply shift this logic to the controller (example in ruby):

obj = {'input#name' => ['val', @person.name], 
  '#my_div' => [%w[addClass error], ['text', @error]]}
  
render js: "$.executeObject( #{obj.to_json} )"
# note that we don't need extra quotes inside function call

and scratch the view altogether. Javascript passed from the server is notoriously hard to debug, so let's pass an object instead. You may also indicate $.executeObject as a callback function for your ajax call

$.post('/my/url', {some: 'data'}, $.executeObject)

and simply pass JSON in response:

render json: obj

In other words, it's always better to pass around data, and not code.

Return value

The function returns the number of jQuery method calls that it was able to perform. Wrong methods or empty selectors fail silently and execution continues.

jquery.execute-object's People

Contributors

punund avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

jquery.execute-object's Issues

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.