Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CiviCRM Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Development
CiviCRM Core
Commits
07e77fbc
Commit
07e77fbc
authored
11 years ago
by
Tim Otten
Browse files
Options
Downloads
Plain Diff
Merge pull request
#1205
from totten/master-backbone-findcreate
CRM-12943 - Add findCreate() helper
parents
bfcb11f6
4bbb9761
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
js/crm.backbone.js
+39
-0
39 additions, 0 deletions
js/crm.backbone.js
tests/qunit/crm-backbone/test.js
+53
-3
53 additions, 3 deletions
tests/qunit/crm-backbone/test.js
with
92 additions
and
3 deletions
js/crm.backbone.js
+
39
−
0
View file @
07e77fbc
...
...
@@ -161,6 +161,45 @@
});
};
/**
* Find a single record, or create a new record.
*
* @param Object options:
* - CollectionClass: class
* - crmCriteria: Object values to search/default on
* - defaults: Object values to put on newly created model (if needed)
* - success: function(model)
* - error: function(collection, error)
*/
CRM
.
Backbone
.
findCreate
=
function
(
options
)
{
options
||
(
options
=
{});
var
collection
=
new
options
.
CollectionClass
([],
{
crmCriteria
:
options
.
crmCriteria
});
collection
.
fetch
({
success
:
function
(
collection
)
{
if
(
collection
.
length
==
0
)
{
var
attrs
=
_
.
extend
({},
collection
.
crmCriteria
,
options
.
defaults
||
{});
var
model
=
collection
.
_prepareModel
(
attrs
,
options
);
options
.
success
(
model
);
}
else
if
(
collection
.
length
==
1
)
{
options
.
success
(
collection
.
first
());
}
else
{
options
.
error
(
collection
,
{
is_error
:
1
,
error_message
:
'
Too many matches
'
});
}
},
error
:
function
(
collection
,
errorData
)
{
if
(
options
.
error
)
{
options
.
error
(
collection
,
errorData
);
}
}
});
};
CRM
.
Backbone
.
Model
=
Backbone
.
Model
.
extend
({
/**
* Return JSON version of model -- but only include fields that are
...
...
This diff is collapsed.
Click to expand it.
tests/qunit/crm-backbone/test.js
+
53
−
3
View file @
07e77fbc
/* ------------ Fixtures/constants ------------ */
var
VALID_CONTACT_ID
=
3
;
var
INVALI
D_CONTACT_ID
=
'
z
'
;
var
MALFORME
D_CONTACT_ID
=
'
z
'
;
var
ContactModel
=
Backbone
.
Model
.
extend
({});
CRM
.
Backbone
.
extendModel
(
ContactModel
,
'
Contact
'
);
...
...
@@ -61,7 +61,7 @@ asyncTest("fetch (ok)", function() {
});
asyncTest
(
"
fetch (error)
"
,
function
()
{
var
c
=
new
ContactModel
({
id
:
INVALI
D_CONTACT_ID
});
var
c
=
new
ContactModel
({
id
:
MALFORME
D_CONTACT_ID
});
c
.
fetch
({
success
:
onUnexpectedSuccess
,
error
:
function
(
model
,
error
)
{
...
...
@@ -212,7 +212,7 @@ asyncTest("fetch by crazy name (0 results)", function() {
asyncTest
(
"
fetch by malformed ID (error)
"
,
function
()
{
var
c
=
new
ContactCollection
([],
{
crmCriteria
:
{
id
:
INVALI
D_CONTACT_ID
id
:
MALFORME
D_CONTACT_ID
}
});
c
.
fetch
({
...
...
@@ -224,3 +224,53 @@ asyncTest("fetch by malformed ID (error)", function() {
});
});
module
(
'
findCreate
'
);
asyncTest
(
"
findCreate by ID (1 result)
"
,
function
()
{
CRM
.
Backbone
.
findCreate
({
CollectionClass
:
ContactCollection
,
crmCriteria
:
{
id
:
VALID_CONTACT_ID
},
error
:
onUnexpectedError
,
success
:
function
(
model
)
{
equal
(
model
.
get
(
'
id
'
),
VALID_CONTACT_ID
);
ok
(
model
.
get
(
'
contact_type
'
)
!=
''
,
'
Expected contact with valid type
'
)
ok
(
model
.
get
(
'
id
'
),
'
Expected contact with valid ID
'
)
start
();
}
});
});
asyncTest
(
"
findCreate by crazy name (0 results) - autocreate
"
,
function
()
{
CRM
.
Backbone
.
findCreate
({
CollectionClass
:
ContactCollection
,
crmCriteria
:
{
organization_name
:
'
asdf23vmlk2309lk2lkasdk-23ASDF32f
'
},
defaults
:
{
contact_type
:
'
Organization
'
},
error
:
onUnexpectedError
,
success
:
function
(
model
)
{
equal
(
model
.
get
(
'
organization_name
'
),
'
asdf23vmlk2309lk2lkasdk-23ASDF32f
'
,
'
Expected default values from crmCriteria
'
);
equal
(
model
.
get
(
'
contact_type
'
),
'
Organization
'
,
'
Expected default values from parameters
'
);
ok
(
!
model
.
get
(
'
id
'
),
'
Expected contact without valid ID
'
)
start
();
}
});
});
asyncTest
(
"
findCreate by malformed ID (error)
"
,
function
()
{
CRM
.
Backbone
.
findCreate
({
CollectionClass
:
ContactCollection
,
crmCriteria
:
{
id
:
MALFORMED_CONTACT_ID
},
success
:
onUnexpectedSuccess
,
error
:
function
(
collection
,
error
)
{
assertApiError
(
error
);
start
();
}
});
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment