javascript

package
v0.27.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package javascript converts go objects and types to javascript code, suitable for embedding in html or sending to the browser via a specialized ajax call.

Index

Examples

Constants

View Source
const JsonObjectType = "goraddObject"

JsonObjectType is used by the ajax processor in goradd.js to indicate that we are sending a special kind of object to the browser. These are things like dates, closures, etc. that are not easily represented by JSON.

Variables

This section is empty.

Functions

func NumberFloat

func NumberFloat(i interface{}) float64

NumberFloat is a helper function to convert an expected float that is returned from a json Unmarshal as a Number, into an actual float64 without returning any errors. If there is an error, it just returns 0. Use this when you absolutely know you are expecting a float. Can convert strings too. Deprecated: Use json.NumberFLoat

func NumberInt

func NumberInt(i interface{}) int

NumberInt is a helper function to convert an expected integer that is returned from a json Unmarshal as a Number, into an actual integer without returning any errors. If there is an error, it just returns 0. Use this when you absolutely know you are expecting an integer. Can convert strings too. Deprecated: Use json.NumberInt

func NumberString

func NumberString(i interface{}) string

NumberString is a helper function to convert a value that might get cast as a Json Number into a string. If there is an error, it just returns 0. Use this when you absolutely know you are expecting a string. Deprecated: Use json.NumberString

func ToJavaScript

func ToJavaScript(v interface{}) string

ToJavaScript will convert the given value to javascript such that it can be embedded in a browser. If it can, it will use the JavaScripter interface to do the conversion. Otherwise, it generally follows json encoding rules. Strings are escaped. Nil pointers become null objects. String maps become javascript objects. To convert a fairly complex object, like a map or slice of objects, convert the inner objects to interfaces

Types

type Arguments

type Arguments []interface{}

Arguments represents a list of javascript function arguments. We can output this as javascript, or as JSON, which gets sent to the goradd javascript during Ajax calls and unpacked there. Primitive types get expressed as constant values in javascript. If you want to represent the name of variable, us a NewJsCode object. NewFunctionCall can be represented using the NewFunctionCall object or the NewClosure object, depending on whether you want the output of the function now, or later.

func (Arguments) JavaScript

func (a Arguments) JavaScript() string

JavaScript implements the JavaScripter interface and returns the arguments as a comma separated list of values suitable to put in JavaScript function arguments.

type Closure

type Closure struct {
	// Body is the body javascript of the Closure
	Body string
	// Args are the names of the arguments in the argument list of the Closure
	Args []string
}

Closure represents a javascript function pointer that can be called by javascript at a later time.

Example
c := NewClosure("return a == b;", "a", "b")
fmt.Println(c.JavaScript())
Output:

function(a, b) {return a == b;}

func NewClosure added in v0.19.0

func NewClosure(body string, args ...string) Closure

NewClosure creates a new Closure object.

func (Closure) JavaScript

func (c Closure) JavaScript() string

JavaScript implements the JavaScripter interface and returns the Closure as javascript code.

func (Closure) MarshalJSON

func (c Closure) MarshalJSON() (buf []byte, err error)

MarshalJSON implements the json.Marshaller interface. The output of this is designed to be unpacked by the goradd.js javascript file during Ajax calls.

type ClosureCall

type ClosureCall struct {
	// Body is the body javascript of the Closure
	Body string
	// Context is what will become the "this" var inside the Closure when called. Specifying "this" will bring the "this" from the outer context in to the Closure.
	Context string
	// Args are the names of the arguments in the argument list of the Closure
	Args []string
}

ClosureCall represents the result of a javascript Closure that is called immediately. context will become the "this" variable inside the Closure when called.

Example
c := NewClosureCall("return this == b;", "a", "b")
fmt.Println(c.JavaScript())
Output:

(function(b) {return this == b;}).call(a)

func NewClosureCall added in v0.19.0

func NewClosureCall(body string, context string, args ...string) ClosureCall

NewClosureCall creates a new ClosureCall.

func (ClosureCall) JavaScript

func (c ClosureCall) JavaScript() string

JavaScript implements the JavaScripter interface and returns the Closure as javascript code.

func (ClosureCall) MarshalJSON

func (c ClosureCall) MarshalJSON() (buf []byte, err error)

MarshalJSON implements the json.Marshaller interface. The output of this is designed to be unpacked by the goradd.js javascript file.

type FunctionCall added in v0.19.0

type FunctionCall struct {
	// Name is the function name
	Name string
	// Context, if given, is the object in the window object which contains the function and is the context for the function.
	// Use dot '.' notation to traverse the object tree. i.e. "obj1.obj2" refers to window.obj1.obj2 in javascript
	Context string
	// Args is the list of arguments of the function call.
	// Strings will be quoted. Use a NewJsCode object to output the name of a javascript variable.
	Args []interface{}
}

FunctionCall represents the result of a function call to a global function or function in an object referenced from global space.

The purpose of this is to immediately use the results of the function call, as opposed to a Closure, which stores a pointer to a function that is used later.

Example
c := NewFunctionCall("substr", "str", 1, 2)
fmt.Println(c.JavaScript())
Output:

str.substr(1,2)

func NewFunctionCall added in v0.19.0

func NewFunctionCall(name string, context string, args ...interface{}) FunctionCall

NewFunctionCall creates a new FunctionCall object.

context will become the "this" value inside the Closure. args will be passed as values, and strings will be quoted. To pass a variable name, wrap the name with a NewJsCode call.

func (FunctionCall) JavaScript added in v0.19.0

func (f FunctionCall) JavaScript() string

JavaScript implements the JavaScripter interface and outputs the function call as embedded JavaScript.

func (FunctionCall) MarshalJSON added in v0.19.0

func (f FunctionCall) MarshalJSON() (buf []byte, err error)

MarshalJSON implements the json.Marshaller interface.

type JavaScripter

type JavaScripter interface {
	JavaScript() string
}

JavaScripter specifies that an object can be converted to javascript (not JSON!). These objects should also be gob encodable and registered with gob, since they might be embedded in a control and need to be serialized.

type JsCode

type JsCode string

JsCode represents straight JavaScript code that should not be escaped or quoted. Normally, string values would be quoted. This outputs a string without quoting or escaping.

func (JsCode) JavaScript added in v0.19.0

func (c JsCode) JavaScript() string

JavaScript implements the JavaScripter interface.

type NoQuoteKey

type NoQuoteKey struct {
	Value interface{}
}

NoQuoteKey is a value wrapper to specify a value in a map whose key should not be quoted when converting to javascript. In some situations, a quoted key has a different meaning from a non-quoted key. For example, when making a list of parameters to pass when calling a javascript command, quoted words are turned into parameters, and non-quoted words are turned into functions. For example, "size" will set the size attribute of the object, and size (no quotes), will call the size() function on the object. i.e. map[string]string {"size":4, "size":NoQuoteKey{JsCode{"obj"}}}

type Undefined

type Undefined struct {
}

Undefined explicitly outputs as "undefined" in javascript. Generally, nil pointers become "null" in javascript, so use this if you would rather have an undefined value.

func (Undefined) JavaScript

func (n Undefined) JavaScript() string

JavaScript implements the JavaScripter interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL